-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessor.vhd
447 lines (390 loc) · 10.8 KB
/
processor.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
445
446
----------------------------------------------------------------------------------
-- Company:
-- Engineer:
--
-- Create Date: 11:09:23 09/20/2013
-- Design Name:
-- Module Name: processor - Behavioral
-- Project Name:
-- Target Devices:
-- Tool versions:
-- Description:
--
-- Dependencies:
--
-- Revision:
-- Revision 0.01 - File Created
-- Additional Comments:
--
----------------------------------------------------------------------------------
library IEEE;
use IEEE.STD_LOGIC_1164.ALL;
use IEEE.STD_LOGIC_ARITH.ALL;
-- Uncomment the following library declaration if using
-- arithmetic functions with Signed or Unsigned values
--use IEEE.NUMERIC_STD.ALL;
-- Uncomment the following library declaration if instantiating
-- any Xilinx primitives in this code.
--library UNISIM;
--use UNISIM.VComponents.all;
library WORK;
use WORK.MIPS_CONSTANT_PKG.ALL;
entity processor is
generic(
MEM_ADDR_BUS : integer := 32;
MEM_DATA_BUS : integer := 32
);
port(
clk : in STD_LOGIC;
reset : in STD_LOGIC;
processor_enable : in STD_LOGIC;
imem_address : out STD_LOGIC_VECTOR (MEM_ADDR_BUS-1 downto 0);
imem_data_in : in STD_LOGIC_VECTOR (MEM_DATA_BUS-1 downto 0);
dmem_data_in : in STD_LOGIC_VECTOR (MEM_DATA_BUS-1 downto 0);
dmem_address : out STD_LOGIC_VECTOR (MEM_ADDR_BUS-1 downto 0);
dmem_address_wr : out STD_LOGIC_VECTOR (MEM_ADDR_BUS-1 downto 0);
dmem_data_out : out STD_LOGIC_VECTOR (MEM_DATA_BUS-1 downto 0);
dmem_write_enable : out STD_LOGIC
);
end processor;
architecture Behavioral of processor is
component register_file is
port(
CLK : in STD_LOGIC;
RESET : in STD_LOGIC;
RW : in STD_LOGIC;
RS_ADDR : in STD_LOGIC_VECTOR (RADDR_BUS-1 downto 0);
RT_ADDR : in STD_LOGIC_VECTOR (RADDR_BUS-1 downto 0);
RD_ADDR : in STD_LOGIC_VECTOR (RADDR_BUS-1 downto 0);
WRITE_DATA : in STD_LOGIC_VECTOR (DDATA_BUS-1 downto 0);
RS : out STD_LOGIC_VECTOR (DDATA_BUS-1 downto 0);
RT : out STD_LOGIC_VECTOR (DDATA_BUS-1 downto 0)
);
end component;
component alu is
generic (N: NATURAL := DDATA_BUS);
port(
X : in STD_LOGIC_VECTOR(N-1 downto 0);
Y : in STD_LOGIC_VECTOR(N-1 downto 0);
ALU_IN : in ALU_INPUT;
R : out STD_LOGIC_VECTOR(N-1 downto 0);
FLAGS : out ALU_FLAGS
);
end component;
component adder is
generic (N: natural := MEM_DATA_BUS);
port(
X : in STD_LOGIC_VECTOR(N-1 downto 0);
Y : in STD_LOGIC_VECTOR(N-1 downto 0);
CIN : in STD_LOGIC;
COUT : out STD_LOGIC;
R : out STD_LOGIC_VECTOR(N-1 downto 0)
);
end component;
-- Instruction signals
signal instruction : STD_LOGIC_VECTOR(31 downto 0);
signal opcode : STD_LOGIC_VECTOR(5 downto 0);
signal rs : STD_LOGIC_VECTOR(4 downto 0);
signal rt : STD_LOGIC_VECTOR(4 downto 0);
signal rd : STD_LOGIC_VECTOR(4 downto 0);
signal shift : STD_LOGIC_VECTOR(4 downto 0);
signal func : STD_LOGIC_VECTOR(5 downto 0);
signal immedi : STD_LOGIC_VECTOR(15 downto 0);
signal target : STD_LOGIC_VECTOR(25 downto 0);
signal immedi_ext : STD_LOGIC_VECTOR(31 downto 0);
-- Register signals
signal reg_rw : STD_LOGIC;
signal reg_rs_addr : STD_LOGIC_VECTOR (RADDR_BUS-1 downto 0);
signal reg_rt_addr : STD_LOGIC_VECTOR (RADDR_BUS-1 downto 0);
signal reg_rd_addr : STD_LOGIC_VECTOR (RADDR_BUS-1 downto 0);
signal reg_write_data : STD_LOGIC_VECTOR (DDATA_BUS-1 downto 0);
signal reg_rs : STD_LOGIC_VECTOR (DDATA_BUS-1 downto 0);
signal reg_rt : STD_LOGIC_VECTOR (DDATA_BUS-1 downto 0);
-- ALU signals
signal alu_x : STD_LOGIC_VECTOR(DDATA_BUS-1 downto 0);
signal alu_y : STD_LOGIC_VECTOR(DDATA_BUS-1 downto 0);
signal alu_in : ALU_INPUT;
signal alu_r : STD_LOGIC_VECTOR(DDATA_BUS-1 downto 0);
signal alu_flags : ALU_FLAGS;
-- Control signals
signal state : STD_LOGIC_VECTOR(1 downto 0);
signal state_next : STD_LOGIC_VECTOR(1 downto 0);
signal alu_src : STD_LOGIC := '0';
signal mem_write : STD_LOGIC := '0';
signal mem_to_reg : STD_LOGIC := '0';
signal reg_write : STD_LOGIC := '0';
signal reg_dst : STD_LOGIC := '0';
signal branch : STD_LOGIC := '0';
signal jump : STD_LOGIC := '0';
-- PC signals
signal pc : STD_LOGIC_VECTOR(MEM_ADDR_BUS-3 downto 0) := (others => '1');
signal pc_1 : STD_LOGIC_VECTOR(MEM_ADDR_BUS-3 downto 0);
signal pc_jump : STD_LOGIC_VECTOR(MEM_ADDR_BUS-3 downto 0);
signal pc_branch : STD_LOGIC_VECTOR(MEM_ADDR_BUS-3 downto 0);
signal pc_next : STD_LOGIC_VECTOR(MEM_ADDR_BUS-3 downto 0);
-- States
constant STATE_FETCH : STD_LOGIC_VECTOR(1 downto 0) := "01";
constant STATE_EXECUTE : STD_LOGIC_VECTOR(1 downto 0) := "10";
constant STATE_STALL : STD_LOGIC_VECTOR(1 downto 0) := "11";
begin
REG_FILE: register_file
port map(
CLK => clk,
RESET => reset,
RW => reg_rw,
RS_ADDR => reg_rs_addr,
RT_ADDR => reg_rt_addr,
RD_ADDR => reg_rd_addr,
WRITE_DATA => reg_write_data,
RS => reg_rs,
RT => reg_rt
);
ALU_THE: alu
generic map (N=>DDATA_BUS)
port map(
X => alu_x,
Y => alu_y,
ALU_IN => alu_in,
R => alu_r,
FLAGS => alu_flags
);
ADDER_PC_1: adder
generic map (N=>MEM_DATA_BUS-2)
port map(
X => pc,
Y => (others => '0'),
CIN => '1',
R => pc_1
);
ADDER_PC_BRANCH: adder
generic map (N=>MEM_DATA_BUS-2)
port map(
X => pc,
Y => immedi_ext(MEM_DATA_BUS-3 downto 0),
CIN => '0',
R => pc_branch
);
CONTROL : process (clk, reset)
begin
if rising_edge(clk) then
-- Reset
if reset = '1' then
state <= STATE_FETCH;
pc <= (others => '0');
elsif processor_enable = '0' then
-- NA
else
case state is
-- Fetch
when STATE_FETCH =>
-- Fetch that instruction!
instruction <= imem_data_in;
-- Update PC
pc <= pc_next;
-- Next: Execute!
state <= STATE_EXECUTE;
-- Execute
when STATE_EXECUTE =>
-- Let stuff decode and execute!
-- Next: Fetch! (or possibly stall)
state <= state_next;
-- Stall
when STATE_STALL =>
-- Give memory some slack!
-- Next: Fetch!
state <= STATE_FETCH;
-- Something went wrong!
when others =>
-- SKY NET RISES!
-- Next: Fetch!
state <= STATE_FETCH;
end case;
end if;
end if;
end process;
SPLITTER : process (instruction)
begin
-- Split Instruction From IMEM
opcode <= instruction(31 downto 26);
rs <= instruction(25 downto 21);
rt <= instruction(20 downto 16);
rd <= instruction(15 downto 11);
shift <= instruction(10 downto 6);
func <= instruction(5 downto 0);
immedi <= instruction(15 downto 0);
target <= instruction(25 downto 0);
end process;
DECODER : process (opcode, func, state)
begin
-- Reset Control signals
alu_src <= '0';
mem_write <= '0';
mem_to_reg <= '0';
reg_write <= '0';
reg_dst <= '0';
branch <= '0';
jump <= '0';
-- Reset ALU function
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '0';
alu_in.Op0 <= '0';
-- Next state (after execute)
state_next <= STATE_FETCH;
case opcode is
-- Tripple register operation
when OP_RRR =>
-- Control signals
alu_src <= '0';
mem_write <= '0';
mem_to_reg <= '0';
reg_write <= '1';
reg_dst <= '1';
branch <= '0';
jump <= '0';
-- ALU function
case func is
when FUNC_ADD =>
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '1';
alu_in.Op0 <= '0';
when FUNC_SUB =>
alu_in.Op3 <= '0';
alu_in.Op2 <= '1';
alu_in.Op1 <= '1';
alu_in.Op0 <= '0';
when FUNC_AND =>
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '0';
alu_in.Op0 <= '0';
when FUNC_OR =>
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '0';
alu_in.Op0 <= '1';
when FUNC_SLT =>
alu_in.Op3 <= '0';
alu_in.Op2 <= '1';
alu_in.Op1 <= '1';
alu_in.Op0 <= '1';
when others =>
null;
end case;
-- Load word ($t = MEM[$s + offset])
when OP_LW =>
-- Control signals
alu_src <= '1';
mem_write <= '0';
mem_to_reg <= '1';
reg_write <= '1';
reg_dst <= '0';
branch <= '0';
jump <= '0';
-- ALU function: Add
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '1';
alu_in.Op0 <= '0';
-- Give it some slack
state_next <= STATE_STALL;
-- Store word (MEM[$s + offset] = $t)
when OP_SW =>
-- Control signals
alu_src <= '1';
mem_write <= '1';
mem_to_reg <= '0';
reg_write <= '0';
reg_dst <= '0';
branch <= '0';
jump <= '0';
-- ALU function: Add
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '1';
alu_in.Op0 <= '0';
-- Load upper immediate
when OP_LUI =>
-- Control signals
alu_src <= '1';
mem_write <= '0';
mem_to_reg <= '0';
reg_write <= '1';
reg_dst <= '0';
branch <= '0';
jump <= '0';
-- ALU function: Shift 16
alu_in.Op3 <= '1';
alu_in.Op2 <= '0';
alu_in.Op1 <= '0';
alu_in.Op0 <= '0';
-- Jump
when OP_J =>
-- Control signals
alu_src <= '0';
mem_write <= '0';
mem_to_reg <= '0';
reg_write <= '0';
reg_dst <= '0';
branch <= '0';
jump <= '1';
-- ALU function: Don't care
alu_in.Op3 <= '0';
alu_in.Op2 <= '0';
alu_in.Op1 <= '0';
alu_in.Op0 <= '0';
-- Branch if equal
when OP_BEQ =>
-- Control signals
alu_src <= '0';
mem_write <= '0';
mem_to_reg <= '0';
reg_write <= '0';
reg_dst <= '0';
branch <= '1';
jump <= '0';
-- ALU function: Sub
alu_in.Op3 <= '0';
alu_in.Op2 <= '1';
alu_in.Op1 <= '1';
alu_in.Op0 <= '0';
when others =>
null;
end case;
-- Fix for R-instructions that reads and writes the same register
if state = STATE_FETCH then
reg_write <= '0';
end if;
end process;
PC_CHOOSER : process(pc_1, pc_jump, pc_branch, jump, branch, alu_flags)
begin
if jump = '1' then
pc_next <= pc_jump;
elsif branch = '1' and alu_flags.Zero = '1' then
pc_next <= pc_branch;
else
pc_next <= pc_1;
end if;
end process;
-- Register File Inputs
reg_rs_addr <= rs;
reg_rt_addr <= rt;
reg_rd_addr <= rd when reg_dst = '1' else rt; -- RegDst Mux
reg_write_data <= dmem_data_in when mem_to_reg = '1' else alu_r; -- MemToReg Mux
reg_rw <= reg_write;
-- ALU Inputs (TODO: FUNC)
alu_x <= reg_rs;
alu_y <= immedi_ext when alu_src = '1' else reg_rt; -- ALUSrc Mux
-- Data Memory Inputs
dmem_address <= alu_r;
dmem_address_wr <= alu_r;
dmem_write_enable <= mem_write;
dmem_data_out <= reg_rt;
-- Immediate Sign Extension
immedi_ext <= ZERO16b & immedi when immedi(15) = '0' else ONE16b & immedi;
-- Jump Target Concatenation
pc_jump <= pc(29 downto 26) & target;
-- Send PC to IMEM
imem_address <= "00" & pc_next;
end Behavioral;