-
Notifications
You must be signed in to change notification settings - Fork 0
/
IITB_Proc.vhd
444 lines (373 loc) · 9.57 KB
/
IITB_Proc.vhd
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
-- Main entity IITB Processor
-- loading libraries
library std;
use std.standard.all;
library ieee;
use ieee.std_logic_1164.all;
library ieee;
use ieee.numeric_std.all;
-- entity declaration
entity IITB_Proc is
port (
clk,rst, inst_flag : in std_logic;
instruc : in std_logic_vector(15 downto 0));
end entity;
architecture main of IITB_Proc is
-- alu component
component ALU is
port( A,B : in std_logic_vector(15 downto 0);
optype : in std_logic ;
result : out std_logic_vector(15 downto 0);
outc, outz: out std_logic);
end component;
-- register file component
component registerfile is
port( add1, add2, add3 : in std_logic_vector(2 downto 0);
data3, pc: in std_logic_vector(15 downto 0);
clock, writ, writ_pc, rst: in std_logic ;
data1, data2: out std_logic_vector(15 downto 0));
end component;
-- memory component
component memory_readwrite is
port (address_in, data_in: in std_logic_vector(15 downto 0);
read_sig, write_sig, clock: in std_logic;
data_out: out std_logic_vector(15 downto 0));
end component;
-- sign extenders
component SignExtender6to16 is
port (inp: in std_logic_vector(5 downto 0);
ext: out std_logic_vector(15 downto 0));
end component;
component SignExtender9to16 is
port (inp: in std_logic_vector(8 downto 0);
optype: in std_logic;
ext: out std_logic_vector(15 downto 0));
end component;
-- state variable
type State is (Sinit, Sinstr, Sx, S0, S1, S2, S3, S4, S5, S6, S7, S8, S9, S10, S11, S12, S13, S14, S15, S16, S17, S18, S19, S20, S21);
signal currstate: State;
-- signals for all other inputs and outputs for components
signal optype: std_logic_vector(3 downto 0);
signal rAdd1, rAdd2, rAdd3: std_logic_vector(2 downto 0);
signal address, mem_add, mdatain, mdataout: std_logic_vector(15 downto 0);
signal t1, t2, t3, instruction: std_logic_vector(15 downto 0);
signal a_alu, b_alu, res_alu : std_logic_vector(15 downto 0);
signal carryout, zeroout : std_logic;
signal rData1, rData2, rData3, rDataPC : std_logic_vector(15 downto 0);
signal ext6in : std_logic_vector(5 downto 0);
signal ext9in : std_logic_vector(8 downto 0);
signal ext6out, ext9out : std_logic_vector(15 downto 0);
signal memread, memwrite, rfilerst, rfwrite : std_logic;
signal carrymain, zeromain : std_logic;
signal alutype, ext9type: std_logic;
begin -- architecture begin
-- components port map
ALU0 : ALU
port map(a_alu, b_alu, alutype, res_alu, carryout, zeroout);
regfile : registerfile
port map(rAdd1, rAdd2, rAdd3, rData3, rDataPC, clk, rfwrite, '0', rfilerst, rData1, rData2);
Memory0: memory_readwrite
port map(mem_add, mdatain, memread, memwrite, clk, mdataout);
Sign6: SignExtender6to16
port map(ext6in, ext6out);
Sign9: SignExtender9to16
port map(ext9in, ext9type, ext9out);
process(clk, currstate) -- process on clock and state
-- state variables
variable nxtstate : State;
variable z, c : std_logic;
variable opr : std_logic_vector(3 downto 0);
variable x1, x2, x3, instr, nxtaddr : std_logic_vector(15 downto 0);
begin -- intial assignments of state variables
nxtstate := currstate;
c:=carrymain; z:= zeromain;
x1 := t1; x2 := t2; x3:=t3;
opr := optype;
nxtaddr := address;
case currstate is -- cases on all states
when Sinit =>
rfilerst <= '1';
memread <= '0';
memwrite <= '0';
rfwrite <= '0';
c := '0'; z := '0';
x1 := "0000000000000000"; x2 := "0000000000000000"; x3 := "0000000000000000";
nxtaddr := "0000000000000000";
nxtstate := S0;
when Sinstr => --write instruction into memory
memwrite <= '1';
rfilerst <= '0';
memread <= '0';
mem_add <= address;
mdatain <= instruc; --writing instruction
nxtstate := Sx; --to increment the address for inserting the instruction
when Sx =>
memwrite <= '0';
rfilerst <= '0';
memread <= '0';
a_alu <= address;
b_alu <= "0000000000000001";
alutype <= '0';
nxtaddr := res_alu;
nxtstate := S0;
when S0 =>
rfilerst <= '0';
memread <= '1';
memwrite <= '0';
rfwrite <= '0';
x1 := "0000000000000000"; x2 := "0000000000000000"; x3 := "0000000000000000";
nxtaddr := address;
instr := mdataout;
opr := instr(15 downto 12);
case (opr) is
when "0000" =>
nxtstate := S1;
when "0001" =>
nxtstate := S2;
when "0010" =>
nxtstate := S1;
when "0011" =>
nxtstate := S3;
when "0100" =>
nxtstate := S5;
when "0101" =>
nxtstate := S5;
when "0110" =>
nxtstate := S10;
when "1001" =>
nxtstate := S9;
when "1100" =>
nxtstate := S1;
when "1000" =>
nxtstate := S9;
when "0111" =>
nxtstate := S10;
when others => null;
end case;
when S1 =>
rfilerst <= '0';
memread <= '0';
memwrite <= '0';
rfwrite <= '0';
rAdd1 <= instr(11 downto 9);
rAdd2 <= instr(8 downto 6);
x1 := rData1;
x2 := rData2;
nxtstate := S4;
when S2 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '0';
rAdd1 <= instr(11 downto 9);
x1 := rData1;
ext6in <= instr(5 downto 0);
x2 := ext6out;
nxtstate := S4;
when S3 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '0';
ext9in <= instr(8 downto 0);
ext9type <= '1';
x1 := ext9out;
nxtstate := S20;
when S4 =>
rfilerst <= '0';
memread <= '0';
memwrite <= '0';
rfwrite <= '0';
a_alu <= x1; b_alu <= x2;
if(opr = "0000" or opr = "0001" or opr = "0100") then
alutype <= '0';
else
alutype <= '1';
end if;
x3 := res_alu;
case (opr) is
when "0000" =>
nxtstate :=S6;
when "0001" =>
nxtstate :=S8;
when "0010" =>
nxtstate :=S6;
when "0100" =>
nxtstate :=S17;
when "0101" =>
nxtstate :=S11;
when "1100" =>
if(x1=x2) then
nxtstate :=S7;
else
nxtstate :=Sx;
end if;
when others => null;
end case;
when S5 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '0';
rAdd1 <= instr(8 downto 6);
ext6in <= instr(5 downto 0);
x1 := rData1;
x2 := ext6out;
nxtstate := S4;
when S6 =>
rfilerst <= '0';
memread <= '0';
memwrite <= '0';
rfwrite <= '1';
if(instr(1 downto 0) = "00" or (instr(1 downto 0) = "10" and c = '1') or (instr(1 downto 0) = "01" and z = '1')) then
rData3 <= x3;
rAdd3 <= instr(5 downto 3);
if(opr = "0000") then
c:= carryout;
end if;
z := zeroout;
end if;
nxtstate := Sx;
when S7 =>
a_alu <= address;
ext6in <= instr(5 downto 0);
b_alu <= ext6out;
alutype <= '0';
nxtaddr := res_alu;
nxtstate := S0;
when S8 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '1';
c := carryout;
z := zeroout;
rData3 <= x3;
rAdd3 <= instr(8 downto 6);
nxtstate := Sx;
when S9 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '1';
rData3 <= address;
rAdd3 <= instr(11 downto 9);
if(opr = "1001") then
nxtstate := S18;
else
nxtstate := S19;
end if;
when S10 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '0';
rAdd1 <= instr(11 downto 9);
x1 := rData1;
if(opr = "0111") then
nxtstate := S16;
else
nxtstate := S12;
end if;
when S11 =>
memwrite <= '1';
memread <= '0';
rfwrite <= '0';
rAdd1 <= instr(11 downto 9);
x2 := rData1;
mem_add <= x3;
mdatain <= x2;
nxtstate := Sx;
when S12 =>
memwrite <= '0';
memread <= '1';
rfwrite <= '0';
mem_add <= x1;
x3 := mdataout;
nxtstate := S14;
when S13 =>
a_alu <= x1;
b_alu <= "0000000000000001";
alutype <= '0';
x1 := res_alu;
if(unsigned(x2)<8) then
if(opr = "0111") then
nxtstate := S16;
else
nxtstate := S12;
end if;
else
nxtstate := Sx;
end if;
when S14 =>
rfwrite <= '1';
rData3 <= x3;
rAdd3 <= x2(2 downto 0);
a_alu <= x2;
b_alu <= "0000000000000001";
alutype <= '0';
x2 := res_alu;
nxtstate := S13;
when S15 =>
a_alu <= x2;
b_alu <= "0000000000000001";
alutype <= '0';
x2 := res_alu;
nxtstate := S13;
when S16 =>
memwrite <= '1';
memread <= '0';
rfwrite <= '0';
rAdd2 <= x2(2 downto 0);
x3 := rData2;
mem_add <= x1;
mdatain <= x3;
nxtstate := S15;
when S17 =>
memread <= '1';
rfwrite <= '1';
c := carryout;
z := zeroout;
mem_add <= x3;
x1 := mdataout;
rData3 <= x1;
rAdd3 <= instr(11 downto 9);
nxtstate := Sx;
when S18 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '0';
rAdd1 <= instr(8 downto 6);
nxtaddr := rData1;
nxtstate := S0;
when S19 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '0';
a_alu <= nxtaddr;
ext9in <= instr(8 downto 0);
ext9type <= '0';
b_alu <= ext9out;
alutype <= '0';
nxtaddr := res_alu;
nxtstate := S0;
when S20 =>
memwrite <= '0';
memread <= '0';
rfwrite <= '1';
rData3 <= x1;
rAdd3 <= instr(11 downto 9);
nxtstate := Sx;
when others => null;
end case; -- cases on state end
if(falling_edge(clk)) then -- if falling edge and reset-> 0, update state
if(rst = '0') then
t1 <= x1; t2 <= x2; t3 <= x3;
carrymain <= c; zeromain <= z;
instruction <= instr;
optype <= opr;
address <= nxtaddr;
currstate <= nxtstate;
if(inst_flag = '1') then --go to Sinstr if instruction is being given
currstate <= Sinstr;
end if;
else
currstate <= Sinit; -- if reset is 1, then go to initial state
end if;
end if;
end process; -- end process
end architecture;