1 |
93 |
oharboe |
-- ZPU
|
2 |
|
|
--
|
3 |
|
|
-- Copyright 2004-2008 oharboe - Øyvind Harboe - oyvind.harboe@zylin.com
|
4 |
|
|
--
|
5 |
|
|
-- The FreeBSD license
|
6 |
|
|
--
|
7 |
|
|
-- Redistribution and use in source and binary forms, with or without
|
8 |
|
|
-- modification, are permitted provided that the following conditions
|
9 |
|
|
-- are met:
|
10 |
|
|
--
|
11 |
|
|
-- 1. Redistributions of source code must retain the above copyright
|
12 |
|
|
-- notice, this list of conditions and the following disclaimer.
|
13 |
|
|
-- 2. Redistributions in binary form must reproduce the above
|
14 |
|
|
-- copyright notice, this list of conditions and the following
|
15 |
|
|
-- disclaimer in the documentation and/or other materials
|
16 |
|
|
-- provided with the distribution.
|
17 |
|
|
--
|
18 |
|
|
-- THIS SOFTWARE IS PROVIDED BY THE ZPU PROJECT ``AS IS'' AND ANY
|
19 |
|
|
-- EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
20 |
|
|
-- THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
21 |
|
|
-- PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
22 |
|
|
-- ZPU PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
23 |
|
|
-- INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
24 |
|
|
-- (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
25 |
|
|
-- OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
26 |
|
|
-- HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
27 |
|
|
-- STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
28 |
|
|
-- ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
29 |
|
|
-- ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
30 |
|
|
--
|
31 |
|
|
-- The views and conclusions contained in the software and documentation
|
32 |
|
|
-- are those of the authors and should not be interpreted as representing
|
33 |
|
|
-- official policies, either expressed or implied, of the ZPU Project.
|
34 |
|
|
|
35 |
|
|
library IEEE;
|
36 |
|
|
use IEEE.STD_LOGIC_1164.ALL;
|
37 |
|
|
use ieee.numeric_std.all;
|
38 |
|
|
|
39 |
|
|
library work;
|
40 |
|
|
use work.zpu_config.all;
|
41 |
|
|
use work.zpupkg.all;
|
42 |
|
|
|
43 |
|
|
|
44 |
|
|
entity zpu_core is
|
45 |
|
|
Port ( clk : in std_logic;
|
46 |
|
|
-- asynchronous reset signal
|
47 |
|
|
areset : in std_logic;
|
48 |
|
|
-- this particular implementation of the ZPU does not
|
49 |
|
|
-- have a clocked enable signal
|
50 |
|
|
enable : in std_logic;
|
51 |
|
|
in_mem_busy : in std_logic;
|
52 |
|
|
mem_read : in std_logic_vector(wordSize-1 downto 0);
|
53 |
|
|
mem_write : out std_logic_vector(wordSize-1 downto 0);
|
54 |
|
|
out_mem_addr : out std_logic_vector(maxAddrBitIncIO downto 0);
|
55 |
|
|
out_mem_writeEnable : out std_logic;
|
56 |
|
|
out_mem_readEnable : out std_logic;
|
57 |
|
|
-- this implementation of the ZPU *always* reads and writes entire
|
58 |
|
|
-- 32 bit words, so mem_writeMask is tied to (others => '1').
|
59 |
|
|
mem_writeMask: out std_logic_vector(wordBytes-1 downto 0);
|
60 |
|
|
-- Set to one to jump to interrupt vector
|
61 |
|
|
-- The ZPU will communicate with the hardware that caused the
|
62 |
|
|
-- interrupt via memory mapped IO or the interrupt flag can
|
63 |
|
|
-- be cleared automatically
|
64 |
|
|
interrupt : in std_logic;
|
65 |
|
|
-- Signal that the break instruction is executed, normally only used
|
66 |
|
|
-- in simulation to stop simulation
|
67 |
|
|
break : out std_logic);
|
68 |
|
|
end zpu_core;
|
69 |
|
|
|
70 |
|
|
architecture behave of zpu_core is
|
71 |
|
|
|
72 |
|
|
signal readIO : std_logic;
|
73 |
|
|
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
signal memAWriteEnable : std_logic;
|
77 |
|
|
signal memAAddr : unsigned(maxAddrBit downto minAddrBit);
|
78 |
|
|
signal memAWrite : unsigned(wordSize-1 downto 0);
|
79 |
|
|
signal memARead : unsigned(wordSize-1 downto 0);
|
80 |
|
|
signal memBWriteEnable : std_logic;
|
81 |
|
|
signal memBAddr : unsigned(maxAddrBit downto minAddrBit);
|
82 |
|
|
signal memBWrite : unsigned(wordSize-1 downto 0);
|
83 |
|
|
signal memBRead : unsigned(wordSize-1 downto 0);
|
84 |
|
|
|
85 |
|
|
|
86 |
|
|
|
87 |
|
|
signal pc : unsigned(maxAddrBit downto 0);
|
88 |
|
|
signal sp : unsigned(maxAddrBit downto minAddrBit);
|
89 |
|
|
|
90 |
|
|
-- this signal is set upon executing an IM instruction
|
91 |
|
|
-- the subsequence IM instruction will then behave differently.
|
92 |
|
|
-- all other instructions will clear the idim_flag.
|
93 |
|
|
-- this yields highly compact immediate instructions.
|
94 |
|
|
signal idim_flag : std_logic;
|
95 |
|
|
|
96 |
|
|
signal busy : std_logic;
|
97 |
|
|
|
98 |
|
|
signal begin_inst : std_logic;
|
99 |
|
|
|
100 |
|
|
|
101 |
|
|
|
102 |
|
|
signal trace_opcode : std_logic_vector(7 downto 0);
|
103 |
|
|
signal trace_pc : std_logic_vector(maxAddrBitIncIO downto 0);
|
104 |
|
|
signal trace_sp : std_logic_vector(maxAddrBitIncIO downto minAddrBit);
|
105 |
|
|
signal trace_topOfStack : std_logic_vector(wordSize-1 downto 0);
|
106 |
|
|
signal trace_topOfStackB : std_logic_vector(wordSize-1 downto 0);
|
107 |
|
|
|
108 |
|
|
-- state machine.
|
109 |
|
|
type State_Type is
|
110 |
|
|
(
|
111 |
|
|
State_Fetch,
|
112 |
|
|
State_WriteIODone,
|
113 |
|
|
State_Execute,
|
114 |
|
|
State_StoreToStack,
|
115 |
|
|
State_Add,
|
116 |
|
|
State_Or,
|
117 |
|
|
State_And,
|
118 |
|
|
State_Store,
|
119 |
|
|
State_ReadIO,
|
120 |
|
|
State_WriteIO,
|
121 |
|
|
State_Load,
|
122 |
|
|
State_FetchNext,
|
123 |
|
|
State_AddSP,
|
124 |
|
|
State_ReadIODone,
|
125 |
|
|
State_Decode,
|
126 |
|
|
State_Resync,
|
127 |
|
|
State_Interrupt
|
128 |
|
|
|
129 |
|
|
);
|
130 |
|
|
|
131 |
|
|
type DecodedOpcodeType is
|
132 |
|
|
(
|
133 |
|
|
Decoded_Nop,
|
134 |
|
|
Decoded_Im,
|
135 |
|
|
Decoded_ImShift,
|
136 |
|
|
Decoded_LoadSP,
|
137 |
|
|
Decoded_StoreSP ,
|
138 |
|
|
Decoded_AddSP,
|
139 |
|
|
Decoded_Emulate,
|
140 |
|
|
Decoded_Break,
|
141 |
|
|
Decoded_PushSP,
|
142 |
|
|
Decoded_PopPC,
|
143 |
|
|
Decoded_Add,
|
144 |
|
|
Decoded_Or,
|
145 |
|
|
Decoded_And,
|
146 |
|
|
Decoded_Load,
|
147 |
|
|
Decoded_Not,
|
148 |
|
|
Decoded_Flip,
|
149 |
|
|
Decoded_Store,
|
150 |
|
|
Decoded_PopSP,
|
151 |
|
|
Decoded_Interrupt
|
152 |
|
|
);
|
153 |
|
|
|
154 |
|
|
|
155 |
|
|
|
156 |
|
|
signal sampledOpcode : std_logic_vector(OpCode_Size-1 downto 0);
|
157 |
|
|
signal opcode : std_logic_vector(OpCode_Size-1 downto 0);
|
158 |
|
|
|
159 |
|
|
signal decodedOpcode : DecodedOpcodeType;
|
160 |
|
|
signal sampledDecodedOpcode : DecodedOpcodeType;
|
161 |
|
|
|
162 |
|
|
|
163 |
|
|
signal state : State_Type;
|
164 |
|
|
|
165 |
|
|
subtype AddrBitBRAM_range is natural range maxAddrBitBRAM downto minAddrBit;
|
166 |
|
|
signal memAAddr_stdlogic : std_logic_vector(AddrBitBRAM_range);
|
167 |
|
|
signal memAWrite_stdlogic : std_logic_vector(memAWrite'range);
|
168 |
|
|
signal memARead_stdlogic : std_logic_vector(memARead'range);
|
169 |
|
|
signal memBAddr_stdlogic : std_logic_vector(AddrBitBRAM_range);
|
170 |
|
|
signal memBWrite_stdlogic : std_logic_vector(memBWrite'range);
|
171 |
|
|
signal memBRead_stdlogic : std_logic_vector(memBRead'range);
|
172 |
|
|
|
173 |
|
|
subtype index is integer range 0 to 3;
|
174 |
|
|
|
175 |
|
|
signal tOpcode_sel : index;
|
176 |
|
|
|
177 |
|
|
|
178 |
|
|
signal inInterrupt : std_logic;
|
179 |
|
|
|
180 |
|
|
|
181 |
|
|
|
182 |
|
|
begin
|
183 |
|
|
|
184 |
|
|
-- generate a trace file.
|
185 |
|
|
--
|
186 |
|
|
-- This is only used in simulation to see what instructions are
|
187 |
|
|
-- executed.
|
188 |
|
|
--
|
189 |
|
|
-- a quick & dirty regression test is then to commit trace files
|
190 |
|
|
-- to CVS and compare the latest trace file against the last known
|
191 |
|
|
-- good trace file
|
192 |
|
|
traceFileGenerate:
|
193 |
|
|
if Generate_Trace generate
|
194 |
|
|
trace_file: trace port map (
|
195 |
|
|
clk => clk,
|
196 |
|
|
begin_inst => begin_inst,
|
197 |
|
|
pc => trace_pc,
|
198 |
|
|
opcode => trace_opcode,
|
199 |
|
|
sp => trace_sp,
|
200 |
|
|
memA => trace_topOfStack,
|
201 |
|
|
memB => trace_topOfStackB,
|
202 |
|
|
busy => busy,
|
203 |
|
|
intsp => (others => 'U')
|
204 |
|
|
);
|
205 |
|
|
end generate;
|
206 |
|
|
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
-- mem_writeMask is not used in this design, tie it to 1
|
210 |
|
|
mem_writeMask <= (others => '1');
|
211 |
|
|
|
212 |
|
|
|
213 |
|
|
|
214 |
|
|
memAAddr_stdlogic <= std_logic_vector(memAAddr(AddrBitBRAM_range));
|
215 |
|
|
memAWrite_stdlogic <= std_logic_vector(memAWrite);
|
216 |
|
|
memBAddr_stdlogic <= std_logic_vector(memBAddr(AddrBitBRAM_range));
|
217 |
|
|
memBWrite_stdlogic <= std_logic_vector(memBWrite);
|
218 |
|
|
|
219 |
|
|
|
220 |
|
|
-- dualport_ram must be defined by the application.
|
221 |
|
|
--
|
222 |
|
|
-- How this can be implemented is highly dependent on the FPGA
|
223 |
|
|
-- and synthesis technology used.
|
224 |
|
|
--
|
225 |
|
|
-- sometimes it can be instantiated as in the
|
226 |
|
|
-- zpu/example/helloworld.vhd, using inference,
|
227 |
|
|
-- but oftentimes it must be instantiated directly
|
228 |
|
|
-- portmapping to part specific FPGA resources
|
229 |
|
|
--
|
230 |
|
|
--
|
231 |
|
|
-- DANGER!!!!!! If inference fails, then synthesis will try
|
232 |
|
|
-- to implement the memory using basic logic resources. This
|
233 |
|
|
-- will almost certainly cause the compiler to get "stuck"
|
234 |
|
|
-- since synthesising such a huge number of basic logic resources
|
235 |
|
|
-- will take more or less forever.
|
236 |
|
|
--
|
237 |
|
|
-- So: if your compiler gets "stuck" then inference is not
|
238 |
|
|
-- the way to go.
|
239 |
|
|
memory: dualport_ram port map (
|
240 |
|
|
clk => clk,
|
241 |
|
|
memAWriteEnable => memAWriteEnable,
|
242 |
|
|
memAAddr => memAAddr_stdlogic,
|
243 |
|
|
memAWrite => memAWrite_stdlogic,
|
244 |
|
|
memARead => memARead_stdlogic,
|
245 |
|
|
memBWriteEnable => memBWriteEnable,
|
246 |
|
|
memBAddr => memBAddr_stdlogic,
|
247 |
|
|
memBWrite => memBWrite_stdlogic,
|
248 |
|
|
memBRead => memBRead_stdlogic
|
249 |
|
|
);
|
250 |
|
|
memARead <= unsigned(memARead_stdlogic);
|
251 |
|
|
memBRead <= unsigned(memBRead_stdlogic);
|
252 |
|
|
|
253 |
|
|
|
254 |
|
|
|
255 |
|
|
tOpcode_sel <= to_integer(pc(minAddrBit-1 downto 0));
|
256 |
|
|
|
257 |
|
|
|
258 |
|
|
|
259 |
|
|
-- move out calculation of the opcode to a seperate process
|
260 |
|
|
-- to make things a bit easier to read
|
261 |
|
|
decodeControl:
|
262 |
|
|
process(memBRead, pc,tOpcode_sel)
|
263 |
|
|
variable tOpcode : std_logic_vector(OpCode_Size-1 downto 0);
|
264 |
|
|
begin
|
265 |
|
|
|
266 |
|
|
-- simplify opcode selection a bit so it passes more synthesizers
|
267 |
|
|
case (tOpcode_sel) is
|
268 |
|
|
|
269 |
|
|
when 0 => tOpcode := std_logic_vector(memBRead(31 downto 24));
|
270 |
|
|
|
271 |
|
|
when 1 => tOpcode := std_logic_vector(memBRead(23 downto 16));
|
272 |
|
|
|
273 |
|
|
when 2 => tOpcode := std_logic_vector(memBRead(15 downto 8));
|
274 |
|
|
|
275 |
|
|
when 3 => tOpcode := std_logic_vector(memBRead(7 downto 0));
|
276 |
|
|
|
277 |
|
|
when others => tOpcode := std_logic_vector(memBRead(7 downto 0));
|
278 |
|
|
end case;
|
279 |
|
|
|
280 |
|
|
sampledOpcode <= tOpcode;
|
281 |
|
|
|
282 |
|
|
if (tOpcode(7 downto 7)=OpCode_Im) then
|
283 |
|
|
sampledDecodedOpcode<=Decoded_Im;
|
284 |
|
|
elsif (tOpcode(7 downto 5)=OpCode_StoreSP) then
|
285 |
|
|
sampledDecodedOpcode<=Decoded_StoreSP;
|
286 |
|
|
elsif (tOpcode(7 downto 5)=OpCode_LoadSP) then
|
287 |
|
|
sampledDecodedOpcode<=Decoded_LoadSP;
|
288 |
|
|
elsif (tOpcode(7 downto 5)=OpCode_Emulate) then
|
289 |
|
|
sampledDecodedOpcode<=Decoded_Emulate;
|
290 |
|
|
elsif (tOpcode(7 downto 4)=OpCode_AddSP) then
|
291 |
|
|
sampledDecodedOpcode<=Decoded_AddSP;
|
292 |
|
|
else
|
293 |
|
|
case tOpcode(3 downto 0) is
|
294 |
|
|
when OpCode_Break =>
|
295 |
|
|
sampledDecodedOpcode<=Decoded_Break;
|
296 |
|
|
when OpCode_PushSP =>
|
297 |
|
|
sampledDecodedOpcode<=Decoded_PushSP;
|
298 |
|
|
when OpCode_PopPC =>
|
299 |
|
|
sampledDecodedOpcode<=Decoded_PopPC;
|
300 |
|
|
when OpCode_Add =>
|
301 |
|
|
sampledDecodedOpcode<=Decoded_Add;
|
302 |
|
|
when OpCode_Or =>
|
303 |
|
|
sampledDecodedOpcode<=Decoded_Or;
|
304 |
|
|
when OpCode_And =>
|
305 |
|
|
sampledDecodedOpcode<=Decoded_And;
|
306 |
|
|
when OpCode_Load =>
|
307 |
|
|
sampledDecodedOpcode<=Decoded_Load;
|
308 |
|
|
when OpCode_Not =>
|
309 |
|
|
sampledDecodedOpcode<=Decoded_Not;
|
310 |
|
|
when OpCode_Flip =>
|
311 |
|
|
sampledDecodedOpcode<=Decoded_Flip;
|
312 |
|
|
when OpCode_Store =>
|
313 |
|
|
sampledDecodedOpcode<=Decoded_Store;
|
314 |
|
|
when OpCode_PopSP =>
|
315 |
|
|
sampledDecodedOpcode<=Decoded_PopSP;
|
316 |
|
|
when others =>
|
317 |
|
|
sampledDecodedOpcode<=Decoded_Nop;
|
318 |
|
|
end case;
|
319 |
|
|
end if;
|
320 |
|
|
end process;
|
321 |
|
|
|
322 |
|
|
|
323 |
|
|
opcodeControl:
|
324 |
|
|
process(clk, areset)
|
325 |
|
|
variable spOffset : unsigned(4 downto 0);
|
326 |
|
|
begin
|
327 |
|
|
if areset = '1' then
|
328 |
|
|
state <= State_Resync;
|
329 |
|
|
break <= '0';
|
330 |
|
|
sp <= unsigned(spStart(maxAddrBit downto minAddrBit));
|
331 |
|
|
pc <= (others => '0');
|
332 |
|
|
idim_flag <= '0';
|
333 |
|
|
begin_inst <= '0';
|
334 |
|
|
memAAddr <= (others => '0');
|
335 |
|
|
memBAddr <= (others => '0');
|
336 |
|
|
memAWriteEnable <= '0';
|
337 |
|
|
memBWriteEnable <= '0';
|
338 |
|
|
out_mem_writeEnable <= '0';
|
339 |
|
|
out_mem_readEnable <= '0';
|
340 |
|
|
memAWrite <= (others => '0');
|
341 |
|
|
memBWrite <= (others => '0');
|
342 |
|
|
inInterrupt <= '0';
|
343 |
|
|
elsif (clk'event and clk = '1') then
|
344 |
|
|
memAWriteEnable <= '0';
|
345 |
|
|
memBWriteEnable <= '0';
|
346 |
|
|
-- This saves ca. 100 LUT's, by explicitly declaring that the
|
347 |
|
|
-- memAWrite can be left at whatever value if memAWriteEnable is
|
348 |
|
|
-- not set.
|
349 |
|
|
memAWrite <= (others => DontCareValue);
|
350 |
|
|
memBWrite <= (others => DontCareValue);
|
351 |
|
|
-- out_mem_addr <= (others => DontCareValue);
|
352 |
|
|
-- mem_write <= (others => DontCareValue);
|
353 |
|
|
spOffset := (others => DontCareValue);
|
354 |
|
|
memAAddr <= (others => DontCareValue);
|
355 |
|
|
memBAddr <= (others => DontCareValue);
|
356 |
|
|
|
357 |
|
|
out_mem_writeEnable <= '0';
|
358 |
|
|
out_mem_readEnable <= '0';
|
359 |
|
|
begin_inst <= '0';
|
360 |
|
|
out_mem_addr <= std_logic_vector(memARead(maxAddrBitIncIO downto 0));
|
361 |
|
|
mem_write <= std_logic_vector(memBRead);
|
362 |
|
|
|
363 |
|
|
decodedOpcode <= sampledDecodedOpcode;
|
364 |
|
|
opcode <= sampledOpcode;
|
365 |
|
|
if interrupt='0' then
|
366 |
|
|
inInterrupt <= '0'; -- no longer in an interrupt
|
367 |
|
|
end if;
|
368 |
|
|
|
369 |
|
|
case state is
|
370 |
|
|
when State_Execute =>
|
371 |
|
|
state <= State_Fetch;
|
372 |
|
|
-- at this point:
|
373 |
|
|
-- memBRead contains opcode word
|
374 |
|
|
-- memARead contains top of stack
|
375 |
|
|
pc <= pc + 1;
|
376 |
|
|
|
377 |
|
|
-- trace
|
378 |
|
|
begin_inst <= '1';
|
379 |
|
|
trace_pc <= (others => '0');
|
380 |
|
|
trace_pc(maxAddrBit downto 0) <= std_logic_vector(pc);
|
381 |
|
|
trace_opcode <= opcode;
|
382 |
|
|
trace_sp <= (others => '0');
|
383 |
|
|
trace_sp(maxAddrBit downto minAddrBit) <= std_logic_vector(sp);
|
384 |
|
|
trace_topOfStack <= std_logic_vector(memARead);
|
385 |
|
|
trace_topOfStackB <= std_logic_vector(memBRead);
|
386 |
|
|
|
387 |
|
|
-- during the next cycle we'll be reading the next opcode
|
388 |
|
|
spOffset(4):=not opcode(4);
|
389 |
|
|
spOffset(3 downto 0) := unsigned(opcode(3 downto 0));
|
390 |
|
|
|
391 |
|
|
idim_flag <= '0';
|
392 |
|
|
case decodedOpcode is
|
393 |
|
|
when Decoded_Interrupt =>
|
394 |
|
|
sp <= sp - 1;
|
395 |
|
|
memAAddr <= sp - 1;
|
396 |
|
|
memAWriteEnable <= '1';
|
397 |
|
|
memAWrite <= (others => DontCareValue);
|
398 |
|
|
memAWrite(maxAddrBit downto 0) <= pc;
|
399 |
|
|
pc <= to_unsigned(32, maxAddrBit+1); -- interrupt address
|
400 |
|
|
report "ZPU jumped to interrupt!" severity note;
|
401 |
|
|
when Decoded_Im =>
|
402 |
|
|
idim_flag <= '1';
|
403 |
|
|
memAWriteEnable <= '1';
|
404 |
|
|
if (idim_flag='0') then
|
405 |
|
|
sp <= sp - 1;
|
406 |
|
|
memAAddr <= sp-1;
|
407 |
|
|
for i in wordSize-1 downto 7 loop
|
408 |
|
|
memAWrite(i) <= opcode(6);
|
409 |
|
|
end loop;
|
410 |
|
|
memAWrite(6 downto 0) <= unsigned(opcode(6 downto 0));
|
411 |
|
|
else
|
412 |
|
|
memAAddr <= sp;
|
413 |
|
|
memAWrite(wordSize-1 downto 7) <= memARead(wordSize-8 downto 0);
|
414 |
|
|
memAWrite(6 downto 0) <= unsigned(opcode(6 downto 0));
|
415 |
|
|
end if;
|
416 |
|
|
when Decoded_StoreSP =>
|
417 |
|
|
memBWriteEnable <= '1';
|
418 |
|
|
memBAddr <= sp+spOffset;
|
419 |
|
|
memBWrite <= memARead;
|
420 |
|
|
sp <= sp + 1;
|
421 |
|
|
state <= State_Resync;
|
422 |
|
|
when Decoded_LoadSP =>
|
423 |
|
|
sp <= sp - 1;
|
424 |
|
|
memAAddr <= sp+spOffset;
|
425 |
|
|
when Decoded_Emulate =>
|
426 |
|
|
sp <= sp - 1;
|
427 |
|
|
memAWriteEnable <= '1';
|
428 |
|
|
memAAddr <= sp - 1;
|
429 |
|
|
memAWrite <= (others => DontCareValue);
|
430 |
|
|
memAWrite(maxAddrBit downto 0) <= pc + 1;
|
431 |
|
|
-- The emulate address is:
|
432 |
|
|
-- 98 7654 3210
|
433 |
|
|
-- 0000 00aa aaa0 0000
|
434 |
|
|
pc <= (others => '0');
|
435 |
|
|
pc(9 downto 5) <= unsigned(opcode(4 downto 0));
|
436 |
|
|
when Decoded_AddSP =>
|
437 |
|
|
memAAddr <= sp;
|
438 |
|
|
memBAddr <= sp+spOffset;
|
439 |
|
|
state <= State_AddSP;
|
440 |
|
|
when Decoded_Break =>
|
441 |
|
|
report "Break instruction encountered" severity failure;
|
442 |
|
|
break <= '1';
|
443 |
|
|
when Decoded_PushSP =>
|
444 |
|
|
memAWriteEnable <= '1';
|
445 |
|
|
memAAddr <= sp - 1;
|
446 |
|
|
sp <= sp - 1;
|
447 |
|
|
memAWrite <= (others => DontCareValue);
|
448 |
|
|
memAWrite(maxAddrBit downto minAddrBit) <= sp;
|
449 |
|
|
when Decoded_PopPC =>
|
450 |
|
|
pc <= memARead(maxAddrBit downto 0);
|
451 |
|
|
sp <= sp + 1;
|
452 |
|
|
state <= State_Resync;
|
453 |
|
|
when Decoded_Add =>
|
454 |
|
|
sp <= sp + 1;
|
455 |
|
|
state <= State_Add;
|
456 |
|
|
when Decoded_Or =>
|
457 |
|
|
sp <= sp + 1;
|
458 |
|
|
state <= State_Or;
|
459 |
|
|
when Decoded_And =>
|
460 |
|
|
sp <= sp + 1;
|
461 |
|
|
state <= State_And;
|
462 |
|
|
when Decoded_Load =>
|
463 |
|
|
if (memARead(ioBit)='1') then
|
464 |
|
|
out_mem_addr <= std_logic_vector(memARead(maxAddrBitIncIO downto 0));
|
465 |
|
|
out_mem_readEnable <= '1';
|
466 |
|
|
state <= State_ReadIO;
|
467 |
|
|
else
|
468 |
|
|
memAAddr <= memARead(maxAddrBit downto minAddrBit);
|
469 |
|
|
end if;
|
470 |
|
|
when Decoded_Not =>
|
471 |
|
|
memAAddr <= sp(maxAddrBit downto minAddrBit);
|
472 |
|
|
memAWriteEnable <= '1';
|
473 |
|
|
memAWrite <= not memARead;
|
474 |
|
|
when Decoded_Flip =>
|
475 |
|
|
memAAddr <= sp(maxAddrBit downto minAddrBit);
|
476 |
|
|
memAWriteEnable <= '1';
|
477 |
|
|
for i in 0 to wordSize-1 loop
|
478 |
|
|
memAWrite(i) <= memARead(wordSize-1-i);
|
479 |
|
|
end loop;
|
480 |
|
|
when Decoded_Store =>
|
481 |
|
|
memBAddr <= sp + 1;
|
482 |
|
|
sp <= sp + 1;
|
483 |
|
|
if (memARead(ioBit)='1') then
|
484 |
|
|
state <= State_WriteIO;
|
485 |
|
|
else
|
486 |
|
|
state <= State_Store;
|
487 |
|
|
end if;
|
488 |
|
|
when Decoded_PopSP =>
|
489 |
|
|
sp <= memARead(maxAddrBit downto minAddrBit);
|
490 |
|
|
state <= State_Resync;
|
491 |
|
|
when Decoded_Nop =>
|
492 |
|
|
memAAddr <= sp;
|
493 |
|
|
when others =>
|
494 |
|
|
null;
|
495 |
|
|
end case;
|
496 |
|
|
when State_ReadIO =>
|
497 |
|
|
if (in_mem_busy = '0') then
|
498 |
|
|
state <= State_Fetch;
|
499 |
|
|
memAWriteEnable <= '1';
|
500 |
|
|
memAWrite <= unsigned(mem_read);
|
501 |
|
|
end if;
|
502 |
|
|
when State_WriteIO =>
|
503 |
|
|
sp <= sp + 1;
|
504 |
|
|
out_mem_writeEnable <= '1';
|
505 |
|
|
out_mem_addr <= std_logic_vector(memARead(maxAddrBitIncIO downto 0));
|
506 |
|
|
mem_write <= std_logic_vector(memBRead);
|
507 |
|
|
state <= State_WriteIODone;
|
508 |
|
|
when State_WriteIODone =>
|
509 |
|
|
if (in_mem_busy = '0') then
|
510 |
|
|
state <= State_Resync;
|
511 |
|
|
end if;
|
512 |
|
|
when State_Fetch =>
|
513 |
|
|
-- We need to resync. During the *next* cycle
|
514 |
|
|
-- we'll fetch the opcode @ pc and thus it will
|
515 |
|
|
-- be available for State_Execute the cycle after
|
516 |
|
|
-- next
|
517 |
|
|
memBAddr <= pc(maxAddrBit downto minAddrBit);
|
518 |
|
|
state <= State_FetchNext;
|
519 |
|
|
when State_FetchNext =>
|
520 |
|
|
-- at this point memARead contains the value that is either
|
521 |
|
|
-- from the top of stack or should be copied to the top of the stack
|
522 |
|
|
memAWriteEnable <= '1';
|
523 |
|
|
memAWrite <= memARead;
|
524 |
|
|
memAAddr <= sp;
|
525 |
|
|
memBAddr <= sp + 1;
|
526 |
|
|
state <= State_Decode;
|
527 |
|
|
when State_Decode =>
|
528 |
|
|
if interrupt='1' and inInterrupt='0' and idim_flag='0' then
|
529 |
|
|
-- We got an interrupt, execute interrupt instead of next instruction
|
530 |
|
|
inInterrupt <= '1';
|
531 |
|
|
decodedOpcode <= Decoded_Interrupt;
|
532 |
|
|
end if;
|
533 |
|
|
-- during the State_Execute cycle we'll be fetching SP+1
|
534 |
|
|
memAAddr <= sp;
|
535 |
|
|
memBAddr <= sp + 1;
|
536 |
|
|
state <= State_Execute;
|
537 |
|
|
when State_Store =>
|
538 |
|
|
sp <= sp + 1;
|
539 |
|
|
memAWriteEnable <= '1';
|
540 |
|
|
memAAddr <= memARead(maxAddrBit downto minAddrBit);
|
541 |
|
|
memAWrite <= memBRead;
|
542 |
|
|
state <= State_Resync;
|
543 |
|
|
when State_AddSP =>
|
544 |
|
|
state <= State_Add;
|
545 |
|
|
when State_Add =>
|
546 |
|
|
memAAddr <= sp;
|
547 |
|
|
memAWriteEnable <= '1';
|
548 |
|
|
memAWrite <= memARead + memBRead;
|
549 |
|
|
state <= State_Fetch;
|
550 |
|
|
when State_Or =>
|
551 |
|
|
memAAddr <= sp;
|
552 |
|
|
memAWriteEnable <= '1';
|
553 |
|
|
memAWrite <= memARead or memBRead;
|
554 |
|
|
state <= State_Fetch;
|
555 |
|
|
when State_Resync =>
|
556 |
|
|
memAAddr <= sp;
|
557 |
|
|
state <= State_Fetch;
|
558 |
|
|
when State_And =>
|
559 |
|
|
memAAddr <= sp;
|
560 |
|
|
memAWriteEnable <= '1';
|
561 |
|
|
memAWrite <= memARead and memBRead;
|
562 |
|
|
state <= State_Fetch;
|
563 |
|
|
when others =>
|
564 |
|
|
null;
|
565 |
|
|
end case;
|
566 |
|
|
|
567 |
|
|
end if;
|
568 |
|
|
end process;
|
569 |
|
|
|
570 |
|
|
|
571 |
|
|
|
572 |
|
|
end behave;
|