1 |
2 |
Valerio63 |
library IEEE;
|
2 |
|
|
use IEEE.std_logic_1164.all; -- defines std_logic types
|
3 |
|
|
use IEEE.STD_LOGIC_unsigned.all;
|
4 |
|
|
use IEEE.STD_LOGIC_arith.all;
|
5 |
|
|
|
6 |
|
|
-- 16 bit program counter register "PC"
|
7 |
|
|
entity pcr is
|
8 |
|
|
port( clk: in STD_LOGIC;
|
9 |
|
|
i: in STD_LOGIC;
|
10 |
|
|
fwait: in STD_LOGIC;
|
11 |
|
|
brk_op: in STD_LOGIC; -- forced BRK (by interrupt request)
|
12 |
|
|
branch_flg: in STD_LOGIC; -- branch flag
|
13 |
|
|
mov_f: in STD_LOGIC; -- MVN/MVP end transfer
|
14 |
|
|
fc: in STD_LOGIC_VECTOR(3 downto 0);
|
15 |
|
|
din1: in STD_LOGIC_VECTOR(7 downto 0);
|
16 |
|
|
din2: in STD_LOGIC_VECTOR(15 downto 0);
|
17 |
|
|
dout: out STD_LOGIC_VECTOR(15 downto 0)
|
18 |
|
|
);
|
19 |
|
|
end pcr;
|
20 |
|
|
|
21 |
|
|
architecture rtl of pcr is
|
22 |
|
|
constant NOP_P: STD_LOGIC_VECTOR(3 downto 0) := "0000"; -- PC no operation
|
23 |
|
|
constant LSB_P: STD_LOGIC_VECTOR(3 downto 0) := "0001"; -- PC load lsb
|
24 |
|
|
constant MSB_P: STD_LOGIC_VECTOR(3 downto 0) := "0010"; -- PC load msb
|
25 |
|
|
constant INC_P: STD_LOGIC_VECTOR(3 downto 0) := "0011"; -- PC increment by 1
|
26 |
|
|
constant LOD_P: STD_LOGIC_VECTOR(3 downto 0) := "0100"; -- PC load lsb\msb (used by JMP\JSR instructions)
|
27 |
|
|
constant LML_P: STD_LOGIC_VECTOR(3 downto 0) := "0101"; -- PC load lsb\msb from oper register (used for JML\JSL instructions)
|
28 |
|
|
constant IN2_P: STD_LOGIC_VECTOR(3 downto 0) := "0110"; -- PC = PC +2 (BRK opcode)
|
29 |
|
|
constant DE3_P: STD_LOGIC_VECTOR(3 downto 0) := "0111"; -- PC = PC -3 (MVN/MVP opcodes)
|
30 |
|
|
constant BRA_P: STD_LOGIC_VECTOR(3 downto 0) := "1000"; -- PC branch
|
31 |
|
|
constant BRL_P: STD_LOGIC_VECTOR(3 downto 0) := "1001"; -- PC branch long
|
32 |
|
|
|
33 |
|
|
signal reg: STD_LOGIC_VECTOR(15 downto 0);
|
34 |
|
|
|
35 |
|
|
begin
|
36 |
|
|
|
37 |
|
|
process(clk)
|
38 |
|
|
begin
|
39 |
|
|
if (clk'event and clk = '1') then
|
40 |
|
|
if fwait = '1' then
|
41 |
|
|
reg <= reg;
|
42 |
|
|
else
|
43 |
|
|
if i = '1' then
|
44 |
|
|
reg <= reg +1;
|
45 |
|
|
else
|
46 |
|
|
case fc is
|
47 |
|
|
when LSB_P => reg(7 downto 0) <= din1; reg(15 downto 8) <= reg(15 downto 8);
|
48 |
|
|
when MSB_P => reg(15 downto 8) <= din1; reg(7 downto 0) <= reg(7 downto 0);
|
49 |
|
|
when INC_P => reg <= reg +1;
|
50 |
|
|
when LOD_P => reg(15 downto 8) <= din1; reg(7 downto 0) <= din2(7 downto 0);
|
51 |
|
|
when BRA_P =>
|
52 |
|
|
if branch_flg = '1' THEN -- if branch taken
|
53 |
|
|
if din1(7) = '0' THEN -- if branch forward
|
54 |
|
|
reg <= reg + "0000000000000001" + ("00000000" & din1);
|
55 |
|
|
else -- if branch backwards
|
56 |
|
|
reg <= reg + "0000000000000001" - ("00000000" & (0 - din1));
|
57 |
|
|
end if;
|
58 |
|
|
else -- if branch not taken
|
59 |
|
|
reg <= reg + "0000000000000001";
|
60 |
|
|
end if;
|
61 |
|
|
when BRL_P =>
|
62 |
|
|
if din1(7) = '0' THEN -- if branch forward
|
63 |
|
|
reg <= reg + "0000000000000001" + (din1 & din2(7 downto 0));
|
64 |
|
|
else -- if branch backwards
|
65 |
|
|
reg <= reg + "0000000000000001" - (0 - (din1 & din2(7 downto 0)));
|
66 |
|
|
end if;
|
67 |
|
|
|
68 |
|
|
when LML_P => reg <= din2;
|
69 |
|
|
when IN2_P =>
|
70 |
|
|
if brk_op = '1' then -- if BRK opcode PC=PC+2
|
71 |
|
|
reg <= reg +1; -- PC already incremented by 1 by cpufsm
|
72 |
|
|
else
|
73 |
|
|
reg <= reg;
|
74 |
|
|
end if;
|
75 |
|
|
when DE3_P =>
|
76 |
|
|
if mov_f = '0' then -- if MVN/MVP transfer not finished
|
77 |
|
|
reg <= reg - "0000000000000011";
|
78 |
|
|
else
|
79 |
|
|
reg <= reg;
|
80 |
|
|
end if;
|
81 |
|
|
when NOP_P => reg <= reg;
|
82 |
|
|
when others => reg <= reg;
|
83 |
|
|
end case;
|
84 |
|
|
end if;
|
85 |
|
|
end if;
|
86 |
|
|
end if;
|
87 |
|
|
end process;
|
88 |
|
|
dout <= reg;
|
89 |
|
|
end rtl;
|
90 |
|
|
|
91 |
|
|
|