-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCOMP2
477 lines (416 loc) · 13 KB
/
SCOMP2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
LIBRARY IEEE;
LIBRARY ALTERA_MF;
LIBRARY LPM;
USE IEEE.STD_LOGIC_1164.ALL;
USE IEEE.STD_LOGIC_ARITH.ALL;
USE IEEE.STD_LOGIC_UNSIGNED.ALL;
USE ALTERA_MF.ALTERA_MF_COMPONENTS.ALL;
USE LPM.LPM_COMPONENTS.ALL;
ENTITY SCOMP IS
PORT(
CLOCK : IN STD_LOGIC;
RESETN : IN STD_LOGIC;
PCINT : IN STD_LOGIC_VECTOR( 3 DOWNTO 0);
IO_WRITE : OUT STD_LOGIC;
IO_CYCLE : OUT STD_LOGIC;
IO_ADDR : OUT STD_LOGIC_VECTOR( 7 DOWNTO 0);
IO_DATA : INOUT STD_LOGIC_VECTOR(15 DOWNTO 0);
--new input/outputs for division, will need buffers
IO_NUM : OUT STD_LOGIC_VECTOR(15 DOWNTO 0);
IO_DENOM : OUT STD_LOGIC_VECTOR(15 DOWNTO 0);
IO_REM : IN STD_LOGIC_VECTOR(15 DOWNTO 0);
IO_QUOT : IN STD_LOGIC_VECTOR(15 DOWNTO 0);
-- MULT BELOW
IO_MULTA : OUT STD_LOGIC_VECTOR(15 DOWNTO 0);
IO_MULTB : OUT STD_LOGIC_VECTOR(15 DOWNTO 0);
IO_MULTP : IN STD_LOGIC_VECTOR(31 DOWNTO 0)
);
END SCOMP;
ARCHITECTURE a OF SCOMP IS
TYPE STATE_TYPE IS (
RESET_PC,
FETCH,
DECODE,
EX_LOAD,
EX_STORE,
EX_STORE2,
EX_ADD,
EX_SUB,
EX_JUMP,
EX_JNEG,
EX_JPOS,
EX_JZERO,
EX_AND,
EX_OR,
EX_XOR,
EX_SHIFT,
EX_ADDI,
EX_ILOAD,
EX_ISTORE,
EX_CALL,
EX_RETURN,
EX_IN,
EX_OUT,
EX_OUT2,
EX_LOADI,
EX_RETI,
--new commands for division
EX_DIVN,
EX_DIVD,
EX_QUOT,
EX_REM,
--new for mult
EX_MULTA,
EX_MULTB,
EX_MULTP
);
TYPE STACK_TYPE IS ARRAY (0 TO 7) OF STD_LOGIC_VECTOR(9 DOWNTO 0);
-- NEW DIVISION stack used to store 16 bit numbers
TYPE BIGSTACK_TYPE IS ARRAY (0 TO 7) OF STD_LOGIC_VECTOR(15 DOWNTO 0);
----------------------------------------------------
SIGNAL STATE : STATE_TYPE;
SIGNAL PC_STACK : STACK_TYPE;
--DIVISION STACKS
SIGNAL Q_STACK : BIGSTACK_TYPE; --Quot
SIGNAL N_STACK : BIGSTACK_TYPE; --Numerator
--SIGNAL D_STACK : BIGSTACK_TYPE; --Denom
SIGNAL R_STACK : BIGSTACK_TYPE; --Rem
--MULTIPLICATION STACKS
SIGNAL MA_STACK : BIGSTACK_TYPE; --MultA
SIGNAL MP_STACK : BIGSTACK_TYPE; --Product of the Mult
SIGNAL HOLDING_STACK : BIGSTACK_TYPE; --Stack used to hold the 32 bit number after it's been cut down to 16 (15)
--
SIGNAL IO_IN : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL AC : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL AC_SAVED : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL AC_SHIFTED : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL IR : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL MDR : STD_LOGIC_VECTOR(15 DOWNTO 0);
SIGNAL PC : STD_LOGIC_VECTOR( 9 DOWNTO 0);
SIGNAL PC_SAVED : STD_LOGIC_VECTOR( 9 DOWNTO 0);
SIGNAL MEM_ADDR : STD_LOGIC_VECTOR( 9 DOWNTO 0);
SIGNAL MW : STD_LOGIC;
SIGNAL IO_WRITE_INT : STD_LOGIC;
SIGNAL GIE : STD_LOGIC;
SIGNAL IIE : STD_LOGIC_VECTOR( 3 DOWNTO 0);
SIGNAL INT_REQ : STD_LOGIC_VECTOR( 3 DOWNTO 0);
SIGNAL INT_REQ_SYNC : STD_LOGIC_VECTOR( 3 DOWNTO 0); -- registered version of INT_REQ
SIGNAL INT_ACK : STD_LOGIC_VECTOR( 3 DOWNTO 0);
SIGNAL IN_HOLD : STD_LOGIC;
BEGIN
-- Use altsyncram component for unified program and data memory
MEMORY : altsyncram
GENERIC MAP (
intended_device_family => "Cyclone",
width_a => 16,
widthad_a => 10,
numwords_a => 1024,
operation_mode => "SINGLE_PORT",
outdata_reg_a => "UNREGISTERED",
indata_aclr_a => "NONE",
wrcontrol_aclr_a => "NONE",
address_aclr_a => "NONE",
outdata_aclr_a => "NONE",
init_file => "SimpleRobotProgram.mif",
lpm_hint => "ENABLE_RUNTIME_MOD=NO",
lpm_type => "altsyncram"
)
PORT MAP (
wren_a => MW,
clock0 => NOT(CLOCK),
address_a => MEM_ADDR,
data_a => AC,
q_a => MDR
);
-- Use LPM function to shift AC using the SHIFT instruction
SHIFTER: LPM_CLSHIFT
GENERIC MAP (
lpm_width => 16,
lpm_widthdist => 4,
lpm_shifttype => "ARITHMETIC"
)
PORT MAP (
data => AC,
distance => IR(3 DOWNTO 0),
direction => IR(4),
result => AC_SHIFTED
);
--------------------------- TRI-STATE DRIVERS
-- Use LPM function to drive I/O bus
IO_BUS: LPM_BUSTRI
GENERIC MAP (
lpm_width => 16
)
PORT MAP (
data => AC,
enabledt => IO_WRITE_INT,
tridata => IO_DATA
);
--
--MOORE state outputs
IO_ADDR <= IR(7 DOWNTO 0);
WITH STATE SELECT MEM_ADDR <=
PC WHEN FETCH,
IR(9 DOWNTO 0) WHEN OTHERS;
WITH STATE SELECT IO_CYCLE <=
'1' WHEN EX_IN,
'1' WHEN EX_OUT2,
---------------------------
'0' WHEN OTHERS;
IO_WRITE <= IO_WRITE_INT;
PROCESS (CLOCK, RESETN)
BEGIN
IF (RESETN = '0') THEN -- Active low, asynchronous reset
STATE <= RESET_PC;
ELSIF (RISING_EDGE(CLOCK)) THEN
CASE STATE IS
WHEN RESET_PC =>
MW <= '0'; -- Clear memory write flag
PC <= "0000000000"; -- Reset PC to the beginning of memory, address 0x000
AC <= x"0000"; -- Clear AC register
IO_WRITE_INT <= '0';
GIE <= '1'; -- Enable interrupts
IIE <= "0000"; -- Mask all interrupts
STATE <= FETCH;
IN_HOLD <= '0';
INT_REQ_SYNC <= "0000";
WHEN FETCH =>
MW <= '0'; -- Clear memory write flag
IR <= MDR; -- Latch instruction into the IR
IO_WRITE_INT <= '0'; -- Lower IO_WRITE after an OUT
-- Interrupt Control
IF (GIE = '1') AND -- If Global Interrupt Enable set and...
(INT_REQ_SYNC /= "0000") THEN -- ...an interrupt is pending
IF INT_REQ_SYNC(0) = '1' THEN -- Got interrupt on PCINT0
INT_ACK <= "0001"; -- Acknowledge the interrupt
PC <= "0000000001"; -- Redirect execution
ELSIF INT_REQ_SYNC(1) = '1' THEN
INT_ACK <= "0010"; -- repeat for other pins
PC <= "0000000010";
ELSIF INT_REQ_SYNC(2) = '1' THEN
INT_ACK <= "0100";
PC <= "0000000011";
ELSIF INT_REQ_SYNC(3) = '1' THEN
INT_ACK <= "1000";
PC <= "0000000100";
END IF;
GIE <= '0'; -- Disable interrupts while in ISR
AC_SAVED <= AC; -- Save AC
PC_SAVED <= PC; -- Save PC
STATE <= FETCH; -- Repeat FETCH with new PC
ELSE -- either no interrupt or interrupts disabled
PC <= PC + 1; -- Increment PC to next instruction address
STATE <= DECODE;
INT_ACK <= "0000"; -- Clear any interrupt acknowledge
END IF;
WHEN DECODE =>
CASE IR(15 downto 10) IS
WHEN "000000" => -- No Operation (NOP)
STATE <= FETCH;
WHEN "000001" => -- LOAD
STATE <= EX_LOAD;
WHEN "000010" => -- STORE
STATE <= EX_STORE;
WHEN "000011" => -- ADD
STATE <= EX_ADD;
WHEN "000100" => -- SUB
STATE <= EX_SUB;
WHEN "000101" => -- JUMP
STATE <= EX_JUMP;
WHEN "000110" => -- JNEG
STATE <= EX_JNEG;
WHEN "000111" => -- JPOS
STATE <= EX_JPOS;
WHEN "001000" => -- JZERO
STATE <= EX_JZERO;
WHEN "001001" => -- AND
STATE <= EX_AND;
WHEN "001010" => -- OR
STATE <= EX_OR;
WHEN "001011" => -- XOR
STATE <= EX_XOR;
WHEN "001100" => -- SHIFT
STATE <= EX_SHIFT;
WHEN "001101" => -- ADDI
STATE <= EX_ADDI;
WHEN "001110" => -- ILOAD
STATE <= EX_ILOAD;
WHEN "001111" => -- ISTORE
STATE <= EX_ISTORE;
WHEN "010000" => -- CALL
STATE <= EX_CALL;
WHEN "010001" => -- RETURN
STATE <= EX_RETURN;
WHEN "010010" => -- IN
STATE <= EX_IN;
WHEN "010011" => -- OUT
STATE <= EX_OUT;
IO_WRITE_INT <= '1'; -- raise IO_WRITE
WHEN "010100" => -- CLI
IIE <= IIE AND NOT(IR(3 DOWNTO 0)); -- disable indicated interrupts
STATE <= FETCH;
WHEN "010101" => -- SEI
IIE <= IIE OR IR(3 DOWNTO 0); -- enable indicated interrupts
STATE <= FETCH;
WHEN "010110" => -- RETI
STATE <= EX_RETI;
WHEN "010111" => -- LOADI
STATE <= EX_LOADI;
WHEN "011000" => -- DIVN start the divide process
STATE <= EX_DIVN;
WHEN "011001" => -- DIV start the divide process
STATE <= EX_DIVD;
WHEN "011010" => --
STATE <= EX_QUOT;
WHEN "011011" => --
STATE <= EX_REM;
WHEN "011100" => --
STATE <= EX_MULTA;
WHEN "011101" => --
STATE <= EX_MULTB;
WHEN "011110" => --
STATE <= EX_MULTP;
WHEN OTHERS =>
STATE <= FETCH; -- Invalid opcodes default to NOP
END CASE;
WHEN EX_LOAD =>
AC <= MDR; -- Latch data from MDR (memory contents) to AC
STATE <= FETCH;
WHEN EX_STORE =>
MW <= '1'; -- Raise MW to write AC to MEM
STATE <= EX_STORE2;
WHEN EX_STORE2 =>
MW <= '0'; -- Drop MW to end write cycle
STATE <= FETCH;
WHEN EX_ADD =>
AC <= AC + MDR; -- MDR MUST be current number from this syntax
STATE <= FETCH;
WHEN EX_SUB =>
AC <= AC - MDR;
STATE <= FETCH;
WHEN EX_JUMP =>
PC <= IR(9 DOWNTO 0);
STATE <= FETCH;
WHEN EX_JNEG =>
IF (AC(15) = '1') THEN
PC <= IR(9 DOWNTO 0);
END IF;
STATE <= FETCH;
WHEN EX_JPOS =>
IF ((AC(15) = '0') AND (AC /= x"0000")) THEN
PC <= IR(9 DOWNTO 0);
END IF;
STATE <= FETCH;
WHEN EX_JZERO =>
IF (AC = x"0000") THEN
PC <= IR(9 DOWNTO 0);
END IF;
STATE <= FETCH;
WHEN EX_AND =>
AC <= AC AND MDR;
STATE <= FETCH;
WHEN EX_OR =>
AC <= AC OR MDR;
STATE <= FETCH;
WHEN EX_XOR =>
AC <= AC XOR MDR;
STATE <= FETCH;
WHEN EX_SHIFT =>
AC <= AC_SHIFTED;
STATE <= FETCH;
WHEN EX_ADDI =>
AC <= AC + (IR(9) & IR(9) & IR(9) & IR(9) &
IR(9) & IR(9) & IR(9 DOWNTO 0));
STATE <= FETCH;
WHEN EX_ILOAD =>
IR(9 DOWNTO 0) <= MDR(9 DOWNTO 0);
STATE <= EX_LOAD;
WHEN EX_ISTORE =>
IR(9 DOWNTO 0) <= MDR(9 DOWNTO 0);
STATE <= EX_STORE;
WHEN EX_CALL =>
FOR i IN 0 TO 6 LOOP
PC_STACK(i + 1) <= PC_STACK(i);
END LOOP;
PC_STACK(0) <= PC;
PC <= IR(9 DOWNTO 0);
STATE <= FETCH;
WHEN EX_RETURN =>
FOR i IN 0 TO 6 LOOP
PC_STACK(i) <= PC_STACK(i + 1);
END LOOP;
PC <= PC_STACK(0);
STATE <= FETCH;
WHEN EX_IN =>
IF IN_HOLD = '0' THEN
AC <= IO_DATA;
IN_HOLD <= '1';
ELSE
STATE <= FETCH;
IN_HOLD <= '0';
END IF;
WHEN EX_OUT =>
STATE <= EX_OUT2;
WHEN EX_OUT2 =>
STATE <= FETCH;
WHEN EX_LOADI =>
AC <= (IR(9) & IR(9) & IR(9) & IR(9) &
IR(9) & IR(9) & IR(9 DOWNTO 0));
STATE <= FETCH;
WHEN EX_RETI =>
GIE <= '1'; -- re-enable interrupts
PC <= PC_SAVED; -- restore saved registers
AC <= AC_SAVED;
STATE <= FETCH;
-- DIVISION CODE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
WHEN EX_DIVN => --FIRST step in the divide process, Logic: take the MDR (input modeled after ADD) and store it in the DIVS signal (?)
N_STACK(0) <= MDR; -- store current number
STATE <= FETCH; -- wait for next command
WHEN EX_DIVD =>
IO_NUM <= N_STACK(0); -- NOW write the DIVS (stored numerator value) and
IO_DENOM <= MDR; -- the JUST input DENOM value to their respective buses
STATE <= EX_QUOT;
WHEN EX_QUOT =>
Q_STACK(0)<= IO_QUOT;
R_STACK(0) <= IO_REM; -- same as QUOT but with the REM value
AC <= Q_STACK(0);
STATE <=FETCH; -- What's in the AC now SHOULD be the quotient of the DIVN and DIVD inputs... I hope.
WHEN EX_REM =>
AC <= R_STACK(0);
STATE <=FETCH; -- What's in the AC now SHOULD be the quotient of the DIVN and DIVD inputs... I hope.
-- MULTIPLICATION CODE <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
WHEN EX_MULTA => --
MA_STACK(0) <= MDR; --
STATE <= FETCH; --
WHEN EX_MULTB =>
IO_MULTA <=MA_STACK(0); --
IO_MULTB <= MDR; --
STATE <= EX_MULTP;
WHEN EX_MULTP =>
MP_STACK(0) <= IO_MULTP(31) & IO_MULTP(14 downto 0);
AC <= MP_STACK(0);
STATE <=FETCH; --
WHEN OTHERS =>
STATE <= FETCH; -- If an invalid state is reached, return to FETCH
END CASE;
INT_REQ_SYNC <= INT_REQ; -- register interrupt requests to SCOMP's clock.
END IF;
END PROCESS;
-- This process monitors the external interrupt pins, setting
-- some flags if a rising edge is detected, and clearing flags
-- once the interrupt is acknowledged.
PROCESS(RESETN, PCINT, INT_ACK, IIE)
BEGIN
IF (RESETN = '0') THEN
INT_REQ <= "0000"; -- clear all interrupts on reset
ELSE
FOR i IN 0 TO 3 LOOP -- for each of the 4 interrupt pins
IF (INT_ACK(i) = '1') OR (IIE(i) = '0') THEN
INT_REQ(i) <= '0'; -- if acknowledged or masked, clear interrupt
ELSIF RISING_EDGE(PCINT(i)) THEN
INT_REQ(i) <= '1'; -- if rising edge on PCINT, request interrupt
END IF;
END LOOP;
END IF;
END PROCESS;
END a;