1 |
2 |
riedelx |
library IEEE;
|
2 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
3 |
|
|
use IEEE.STD_LOGIC_ARITH.ALL;
|
4 |
|
|
use IEEE.STD_LOGIC_UNSIGNED.ALL;
|
5 |
|
|
|
6 |
|
|
use work.TinyXconfig.ALL;
|
7 |
|
|
-- Uncomment the following lines to use the declarations that are
|
8 |
|
|
-- provided for instantiating Xilinx primitive components.
|
9 |
|
|
--library UNISIM;
|
10 |
|
|
--use UNISIM.VComponents.all;
|
11 |
|
|
|
12 |
|
|
entity TinyX is
|
13 |
|
|
Port ( dataout : out cpuWord;
|
14 |
|
|
datain : in cpuWord;
|
15 |
|
|
adr : out std_logic_vector(15 downto 0);
|
16 |
|
|
wrn : out std_logic;
|
17 |
|
|
rdn : out std_logic;
|
18 |
|
|
clock : in std_logic;
|
19 |
|
|
clrn : in std_logic);
|
20 |
|
|
|
21 |
|
|
end TinyX;
|
22 |
|
|
|
23 |
|
|
architecture Behavioral of TinyX is
|
24 |
|
|
|
25 |
|
|
constant CPU_S0 : std_logic_vector(1 downto 0) := "00";
|
26 |
|
|
constant CPU_S1 : std_logic_vector(1 downto 0) := "01";
|
27 |
|
|
constant CPU_S2 : std_logic_vector(1 downto 0) := "10";
|
28 |
|
|
|
29 |
|
|
signal r0 : cpuWord; -- register 0
|
30 |
|
|
signal r1 : cpuWord; -- register 1
|
31 |
|
|
signal r2 : cpuWord; -- register 2
|
32 |
|
|
signal r3 : cpuWord; -- register 3
|
33 |
|
|
signal r4 : cpuWord; -- register 4
|
34 |
|
|
signal r5 : cpuWord; -- register 5
|
35 |
|
|
signal r6 : cpuWord; -- register 6
|
36 |
|
|
signal r7 : cpuWord; -- register 7 AND PROGRAM COUNTER !!!
|
37 |
|
|
signal imm : cpuWord;
|
38 |
|
|
signal abus : cpuWord; -- bus to alu opa and tristate buffer input
|
39 |
|
|
signal bbus : cpuWord; -- bus from valmux to alu opb
|
40 |
|
|
signal adrbus : cpuWord;
|
41 |
|
|
signal dstbus : cpuWord; -- from memmux to register bank
|
42 |
|
|
signal result : cpuWord; -- from alu res to memmux
|
43 |
|
|
signal carryIn : std_logic; -- carry to alu
|
44 |
|
|
signal flagC : std_logic;
|
45 |
|
|
signal flagZ : std_logic;
|
46 |
|
|
signal flagV : std_logic;
|
47 |
|
|
signal flagN : std_logic;
|
48 |
|
|
signal regC : std_logic; -- registered carry flag
|
49 |
|
|
signal regZ : std_logic; -- registered zero flag
|
50 |
|
|
signal regV : std_logic; -- registered overflow flag
|
51 |
|
|
signal regN : std_logic; -- registered negaive flag
|
52 |
|
|
signal aluMode : std_logic_vector(3 downto 0);
|
53 |
|
|
signal ccMode : std_logic_vector(3 downto 0);
|
54 |
|
|
signal flagBit : std_logic; -- result of condition code comparision
|
55 |
|
|
signal cpuState : std_logic_vector(1 downto 0); -- CPU state counter
|
56 |
|
|
signal carryUse : std_logic;
|
57 |
|
|
signal flagUpdate : std_logic;
|
58 |
|
|
signal writeCycle : std_logic;
|
59 |
|
|
signal dstclk : std_logic_vector(2 downto 0);
|
60 |
|
|
signal sela : std_logic_vector(2 downto 0);
|
61 |
|
|
signal selb : std_logic_vector(2 downto 0);
|
62 |
|
|
signal selm : std_logic;
|
63 |
|
|
signal selv : std_logic;
|
64 |
|
|
|
65 |
|
|
component cctest port ( fN : in std_logic;
|
66 |
|
|
fV : in std_logic;
|
67 |
|
|
fC : in std_logic;
|
68 |
|
|
fZ : in std_logic;
|
69 |
|
|
what : in std_logic_vector(3 downto 0);
|
70 |
|
|
result : out std_logic);
|
71 |
|
|
end component;
|
72 |
|
|
|
73 |
|
|
component ALU Port ( opa : in cpuWord;
|
74 |
|
|
opb : in cpuWord;
|
75 |
|
|
res : out cpuWord;
|
76 |
|
|
cin : in std_logic;
|
77 |
|
|
cout : out std_logic;
|
78 |
|
|
zero : out std_logic;
|
79 |
|
|
sign : out std_logic;
|
80 |
|
|
over : out std_logic;
|
81 |
|
|
what : in std_logic_vector(3 downto 0));
|
82 |
|
|
end component;
|
83 |
|
|
|
84 |
|
|
component mux2 Port ( ina : in cpuWord;
|
85 |
|
|
inb : in cpuWord;
|
86 |
|
|
mout : out cpuWord;
|
87 |
|
|
sel : in std_logic);
|
88 |
|
|
end component;
|
89 |
|
|
|
90 |
|
|
component mux8 Port ( ina : in cpuWord;
|
91 |
|
|
inb : in cpuWord;
|
92 |
|
|
inc : in cpuWord;
|
93 |
|
|
ind : in cpuWord;
|
94 |
|
|
ine : in cpuWord;
|
95 |
|
|
inf : in cpuWord;
|
96 |
|
|
ing : in cpuWord;
|
97 |
|
|
inh : in cpuWord;
|
98 |
|
|
mout : out cpuWord;
|
99 |
|
|
sel : in std_logic_vector(2 downto 0));
|
100 |
|
|
end component;
|
101 |
|
|
|
102 |
|
|
begin
|
103 |
|
|
|
104 |
|
|
assert XLEN > 31 report "XLEN must at least 32 and multiple of 8";
|
105 |
|
|
assert (XLEN rem 8) = 0 report "XLEN must at least 32 and multiple of 8";
|
106 |
|
|
|
107 |
|
|
--####### condition code comparison ##################################################
|
108 |
|
|
flagger : cctest port map(regN, regV, regC, regZ, ccMode, flagBit);
|
109 |
|
|
|
110 |
|
|
carryIn <= regC and carryUse;
|
111 |
|
|
--####### alu processing #############################################################
|
112 |
|
|
alucell : ALU port map(abus, bbus, result, carryIn, flagC, flagZ, flagN, flagV, aluMode);
|
113 |
|
|
|
114 |
|
|
--####### multiplexor opamux #########################################################
|
115 |
|
|
opamux : mux8 port map(r0, r1, r2, r3, r4, r5, r6, r7, abus, sela);
|
116 |
|
|
|
117 |
|
|
--####### multiplexor opbmux #########################################################
|
118 |
|
|
opbmux : mux8 port map(r0, r1, r2, r3, r4, r5, r6, r7, adrbus, selb);
|
119 |
|
|
|
120 |
|
|
--####### multiplexor valmux #########################################################
|
121 |
|
|
valmux : mux2 port map(adrbus, imm, bbus, selv);
|
122 |
|
|
|
123 |
|
|
--####### multiplexor memmux #########################################################
|
124 |
|
|
memmux : mux2 port map(result, datain, dstbus, selm);
|
125 |
|
|
|
126 |
|
|
adr <= adrbus(15 downto 0); -- drive the address bus asynchronusly
|
127 |
|
|
dataout <= abus; -- also the dataout bus
|
128 |
|
|
|
129 |
|
|
process (clock, clrn)
|
130 |
|
|
begin
|
131 |
|
|
if clrn = '0' then -- reset the CPU
|
132 |
|
|
cpuState <= CPU_S0;
|
133 |
|
|
r0 <= getStdLogicVectorZeroes(XLEN);
|
134 |
|
|
r1 <= getStdLogicVectorZeroes(XLEN);
|
135 |
|
|
r2 <= getStdLogicVectorZeroes(XLEN);
|
136 |
|
|
r3 <= getStdLogicVectorZeroes(XLEN);
|
137 |
|
|
r4 <= getStdLogicVectorZeroes(XLEN);
|
138 |
|
|
r5 <= getStdLogicVectorZeroes(XLEN);
|
139 |
|
|
r6 <= getStdLogicVectorZeroes(XLEN);
|
140 |
|
|
r7 <= getStdLogicVectorZeroes(XLEN); -- set pc to starting address
|
141 |
|
|
-- feed the multiplexors
|
142 |
|
|
sela <= "111";
|
143 |
|
|
selb <= "111";
|
144 |
|
|
selv <= '0';
|
145 |
|
|
selm <= '0';
|
146 |
|
|
carryUse <= '0';
|
147 |
|
|
|
148 |
|
|
ccMode <= "0000";
|
149 |
|
|
aluMode <= "0000";
|
150 |
|
|
regN <= '0';
|
151 |
|
|
regV <= '0';
|
152 |
|
|
regC <= '0';
|
153 |
|
|
regZ <= '0';
|
154 |
|
|
wrn <= '1'; -- read access
|
155 |
|
|
rdn <= '0';
|
156 |
|
|
else -- normal operation of CPU
|
157 |
|
|
if clock'event and clock = '1' then -- rising clock edge
|
158 |
|
|
case cpuState is
|
159 |
|
|
when CPU_S0 => --####### S0 #######################################
|
160 |
|
|
ccMode <= datain(ccModeLeft downto ccModeRight);
|
161 |
|
|
aluMode <= datain(aluModeLeft downto aluModeRight);
|
162 |
|
|
selm <= datain(memmuxBit);
|
163 |
|
|
dstclk <= datain(dstClkLeft downto dstClkRight);
|
164 |
|
|
writeCycle <= datain(writeCycleBit);
|
165 |
|
|
sela <= datain(opamuxLeft downto opamuxRight);
|
166 |
|
|
selv <= datain(valmuxBit);
|
167 |
|
|
selb <= datain(opbmuxLeft downto opbmuxRight);
|
168 |
|
|
flagUpdate <= datain(flagUpdateBit);
|
169 |
|
|
carryUse <= datain(carryUseBit);
|
170 |
|
|
imm <= "000000000000000000000000" & datain(immediateLeft downto 0);
|
171 |
|
|
if datain(writeCycleBit) = '0' then
|
172 |
|
|
rdn <= '0';
|
173 |
|
|
wrn <= '1';
|
174 |
|
|
else
|
175 |
|
|
rdn <= '1';
|
176 |
|
|
wrn <= '0';
|
177 |
|
|
end if;
|
178 |
|
|
cpuState <= CPU_S1;
|
179 |
|
|
when CPU_S1 => --####### S1 #######################################
|
180 |
|
|
-- latch the alu result and the flags
|
181 |
|
|
if writeCycle = '0' then
|
182 |
|
|
if flagUpdate = '1' then
|
183 |
|
|
regC <= flagC;
|
184 |
|
|
regZ <= flagZ;
|
185 |
|
|
regV <= flagV;
|
186 |
|
|
regN <= flagN;
|
187 |
|
|
end if;
|
188 |
|
|
if flagBit = '1' then -- save the alu result, only at read cycle ???
|
189 |
|
|
case dstclk is -- select destination register
|
190 |
|
|
when "000" =>
|
191 |
|
|
r0 <= dstbus;
|
192 |
|
|
when "001" =>
|
193 |
|
|
r1 <= dstbus;
|
194 |
|
|
when "010" =>
|
195 |
|
|
r2 <= dstbus;
|
196 |
|
|
when "011" =>
|
197 |
|
|
r3 <= dstbus;
|
198 |
|
|
when "100" =>
|
199 |
|
|
r4 <= dstbus;
|
200 |
|
|
when "101" =>
|
201 |
|
|
r5 <= dstbus;
|
202 |
|
|
when "110" =>
|
203 |
|
|
r6 <= dstbus;
|
204 |
|
|
when "111" =>
|
205 |
|
|
r7 <= dstbus;
|
206 |
|
|
when others =>
|
207 |
|
|
r0 <= dstbus;
|
208 |
|
|
end case; -- destination register selection
|
209 |
|
|
end if; -- flagBit
|
210 |
|
|
end if;
|
211 |
|
|
rdn <= '0';
|
212 |
|
|
wrn <= '1';
|
213 |
|
|
-- drive the adr bus, set read pulse
|
214 |
|
|
carryUse <= '0';
|
215 |
|
|
ccMode <= "1111";
|
216 |
|
|
aluMode <= "1011";
|
217 |
|
|
sela <= "111";
|
218 |
|
|
selb <= "111"; -- pc to adr bus
|
219 |
|
|
selv <= '0';
|
220 |
|
|
selm <= '0';
|
221 |
|
|
cpuState <= CPU_S2;
|
222 |
|
|
when CPU_S2 => --####### S2 #######################################
|
223 |
|
|
r7 <= dstbus; -- store incremented pc
|
224 |
|
|
-- data bus drives the muxes and enables
|
225 |
|
|
ccMode <= datain(ccModeLeft downto ccModeRight);
|
226 |
|
|
aluMode <= datain(aluModeLeft downto aluModeRight);
|
227 |
|
|
selm <= datain(memmuxBit);
|
228 |
|
|
dstclk <= datain(dstClkLeft downto dstClkRight);
|
229 |
|
|
writeCycle <= datain(writeCycleBit);
|
230 |
|
|
sela <= datain(opamuxLeft downto opamuxRight);
|
231 |
|
|
selv <= datain(valmuxBit);
|
232 |
|
|
selb <= datain(opbmuxLeft downto opbmuxRight);
|
233 |
|
|
flagUpdate <= datain(flagUpdateBit);
|
234 |
|
|
carryUse <= datain(carryUseBit);
|
235 |
|
|
imm <= "000000000000000000000000" & datain(immediateLeft downto 0);
|
236 |
|
|
if datain(writeCycleBit) = '0' then
|
237 |
|
|
rdn <= '0';
|
238 |
|
|
wrn <= '1';
|
239 |
|
|
else
|
240 |
|
|
rdn <= '1';
|
241 |
|
|
wrn <= '0';
|
242 |
|
|
end if;
|
243 |
|
|
cpuState <= CPU_S1;
|
244 |
|
|
when others =>
|
245 |
|
|
cpuState <= CPU_S0;
|
246 |
|
|
end case; -- cpuState
|
247 |
|
|
end if; -- rising clock edge
|
248 |
|
|
end if; -- clrn = 0
|
249 |
|
|
end process; -- clock, clrn
|
250 |
|
|
end Behavioral;
|
251 |
|
|
|