-
Notifications
You must be signed in to change notification settings - Fork 0
/
single_cycle_controller.vhd
183 lines (150 loc) · 5.72 KB
/
single_cycle_controller.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
library ieee;
use ieee.std_logic_1164.all;
use ieee.numeric_std.all;
entity single_cycle_controller is
port
(
instruction : in std_logic_vector(31 downto 0);
RegDst,ExtOp,ALUSrc,MemtoReg,RegWr,shdir,MemWr,Branch,Jump,JumpReg,z_controller,JAL,LUI : out std_logic; --signals from controller
ALUctr : out std_logic_vector(3 downto 0);
byteena : out std_logic_vector(3 downto 0);
z : in std_logic
);
end single_cycle_controller;
architecture arch of single_cycle_controller is
signal func : std_logic_vector(5 downto 0);
signal ALUop : std_logic_vector(2 downto 0);
signal opcode : std_logic_vector(5 downto 0);
begin
ALU32CONTROL : entity work.alu32control --ALUctr generation
port map
(
func => func,
ALUop => ALUop,
control => ALUctr
);
process(opcode,func,z)
begin
RegDst <= '1'; --default is Rd
ExtOp <= 'X'; --dont care unless using extender
ALUSrc <= '0'; --default busB is register, otherwise sign extended immediate value
MemtoReg <= '0'; --default ALU result is writeback
RegWr <= '1'; --default is write back value
shdir <= 'X'; --only used during shifts
MemWr <= '0'; --default reading from data memory
Branch <= '0'; --default do not branch
Jump <= '0'; --default do not jump
JumpReg <= '0'; --default do not jump from register
JAL <= '0';
LUI <= '0';
ALUop <= "XXX";
z_controller <= z;
byteena <= (others => '1'); --default reading word from data memory
ALUop <= (others => '0');
case opcode is
when "000000" => --R type function, ALU32control will determine ALUctr from func
ALUop <= "010"; --use func
case func is
when "001000" => --jump register
Jump <= '1';
JumpReg <= '1';
RegWr <= '0';
when "000000" => --shift left logical
shdir <= '0'; --left
when "000010" => --shift right logical
shdir <= '1'; --right
when others => --add,addu,and,nor,or,slt,sltu,sub,subu all use default values
null;
end case;
when "001000" => --add immediate signed
ALUop <= "000"; --add
ExtOp <= '1'; --signed extension
ALUSrc <= '1'; --immediate value is second operand
RegDst <= '0'; --t is destination for immediates
when "001001" => --add immediate unsigned turns out it just doesn't execute a trap, which this CPU doesn't support
ALUop <= "000"; --add
ExtOp <= '0'; --signed extension
ALUSrc <= '1'; --immediate value is second operand
RegDst <= '0'; --t is destination for immediates
when "001100" => --and immediate
ALUop <= "011"; --and
ExtOp <= '0'; --zero extension
ALUSrc <= '1'; --immediate value is second operand
RegDst <= '0'; --t is destination for immediates
when "000100" => --beq
ALUop <= "001"; --subtract
Branch <= '1'; --branch if z is true, no need to not z
RegWr <= '0'; --do not write back value
when "000101" => --bne
ALUop <= "001"; --subtract
Branch <= '1'; --branch if z is false
z_controller <= not z; --if z is false we'll branch
RegWr <= '0'; --do not write back value
when "000010" => --jump
Jump <= '1';
RegWr <= '0'; --do not write back value
when "000011" => --jal
Jump <= '1';
JAL <= '1';
when "100100" => --load byte unsigned
ALUop <= "000"; --add to get address offset
byteena <= "0001"; --only enable one byte from memory
MemtoReg <= '1'; --result from memory not ALU
ExtOp <= '1'; --signed extension on immediate value
ALUsrc <= '1'; --immediate operand
RegDst <= '0'; --t is destination for loads
when "100101" => --load halfword unsigned
ALUop <= "000"; --add to get address offset
byteena <= "0001"; --only enable 2 bytes from memory
MemtoReg <= '1'; --result from memory not ALU
ExtOp <= '1'; --signed extension on immediate value
ALUsrc <= '1'; --immediate operand
RegDst <= '0'; --t is destination for loads
when "001111" => --lui
LUI <= '1'; --select immediate value to be written back with padded 16 lower half zeros, before sign extender
RegDst <= '0'; --t is destination for loads
when "100011" => --load word
ALUop <= "000"; --add to get address offset, default byteena of 1111 is good
MemtoReg <= '1'; --result from memory not ALU
ExtOp <= '1'; --signed extension on immediate value
ALUsrc <= '1'; --immediate operand
RegDst <= '0'; --t is destination for loads
when "001101" => --or immediate
ALUop <= "100"; --or
ExtOp <= '0'; --zero extension
ALUsrc <= '1'; --immediate value is second operand
RegDst <= '0'; --t is destination for immediates
when "001010" => --slti
ALUop <= "101"; --slt
ExtOp <= '1'; --signed comparison
ALUsrc <= '1'; --immediate operand
RegDst <= '0'; --t is destination for immediates
when "001011" => --sltiu
ALUop <= "110"; --sltu
ExtOp <= '0'; --unsigned comparison
ALUsrc <= '1'; --immediate operand
RegDst <= '0'; --t is destination for immediates
when "101000" => --store byte
ALUop <= "000"; --add to get address offset
byteena <= "0001"; --only enable one byte to memory
MemWr <= '1'; --write operation
RegWr <= '0'; --do not writeback reg
ExtOp <= '1'; --signed extension on immediate value
when "101001" => --store halfword
ALUop <= "000"; --add to get address offset
byteena <= "0011"; --only enable 2 bytes to memory
MemWr <= '1'; --write operation
RegWr <= '0'; --do not writeback reg
ExtOp <= '1'; --signed extension on immediate value
when "101011" => --store word
ALUop <= "000"; --add to get address offset, all bytes enabled
MemWr <= '1'; --write operation
RegWr <= '0'; --do not writeback reg
ExtOp <= '1'; --signed extension on immediate value
when others =>
null;
end case;
end process;
func <= instruction(5 downto 0);
opcode <= instruction(31 downto 26);
end architecture arch;