1 |
2 |
ultro |
module sub86( CLK, RSTN, IA, ID, A, D, Q, WEN,BEN );
|
2 |
3 |
ultro |
input CLK,RSTN;
|
3 |
2 |
ultro |
output [31:0] IA;
|
4 |
|
|
input [15:0] ID;
|
5 |
|
|
output [31:0] A;
|
6 |
|
|
input [31:0] D;
|
7 |
|
|
output [31:0] Q;
|
8 |
|
|
output WEN;
|
9 |
|
|
output [1:0] BEN;
|
10 |
|
|
wire nncry,neqF,ngF,nlF;
|
11 |
|
|
reg [31:0] EAX,EBX,ECX,EDX,EBP,ESP,PC,regsrc,regdest,alu_out;
|
12 |
|
|
reg [4:0] state,nstate;
|
13 |
3 |
ultro |
wire [4:0] shtr;
|
14 |
2 |
ultro |
reg [2:0] src,dest;
|
15 |
3 |
ultro |
reg cry,ncry,prefx,nprefx,cmpr,eqF,gF,lF;
|
16 |
2 |
ultro |
wire [31:0] incPC,Sregsrc,Zregsrc,pc_jg,pc_jge,pc_jl,pc_jle,pc_eq,pc_jp,pc_neq;
|
17 |
|
|
wire signed [31:0] sft_in,sft_out,tst;
|
18 |
3 |
ultro |
wire [32:0] adder_out,sub_out;
|
19 |
2 |
ultro |
`define fetch 5'b00000
|
20 |
|
|
`define jmp 5'b00001
|
21 |
|
|
`define jmp2 5'b00010
|
22 |
|
|
`define jge 5'b00011
|
23 |
|
|
`define jge2 5'b00100
|
24 |
|
|
`define imm 5'b00101
|
25 |
|
|
`define imm2 5'b00110
|
26 |
|
|
`define lea 5'b00111
|
27 |
|
|
`define lea2 5'b01000
|
28 |
|
|
`define call 5'b01001
|
29 |
|
|
`define call2 5'b01010
|
30 |
|
|
`define ret 5'b01011
|
31 |
|
|
`define ret2 5'b01100
|
32 |
|
|
`define shift 5'b01110
|
33 |
|
|
`define jg 5'b01111
|
34 |
|
|
`define jg2 5'b10000
|
35 |
|
|
`define jl 5'b10001
|
36 |
|
|
`define jl2 5'b10010
|
37 |
|
|
`define jle 5'b10011
|
38 |
|
|
`define jle2 5'b10100
|
39 |
|
|
`define je 5'b10101
|
40 |
|
|
`define je2 5'b10110
|
41 |
|
|
`define jne 5'b10111
|
42 |
|
|
`define jne2 5'b11000
|
43 |
3 |
ultro |
`define mul 5'b11001
|
44 |
|
|
`define mul2 5'b11010
|
45 |
2 |
ultro |
always @(posedge CLK or negedge RSTN)
|
46 |
|
|
if(!RSTN) begin
|
47 |
4 |
ultro |
ESP <= 32'b011111111; PC <= 32'b0;
|
48 |
|
|
eqF <= 1'b0 ; lF <= 1'b0 ; gF <= 1'b0;
|
49 |
|
|
state <=5'b0; prefx <= 1'b0; cry <= 1'b0;
|
50 |
2 |
ultro |
end
|
51 |
|
|
else
|
52 |
|
|
begin
|
53 |
|
|
state <= nstate; prefx <= nprefx; cry <= ncry;
|
54 |
|
|
case (cmpr)
|
55 |
|
|
1'b1 : begin eqF <= neqF ; lF <= nlF; gF <= ngF; end
|
56 |
|
|
default: begin eqF <= eqF ; lF <= lF; gF <= gF; end
|
57 |
|
|
endcase
|
58 |
|
|
if ((state==`fetch) || (state==`ret))
|
59 |
|
|
begin
|
60 |
|
|
if (dest==3'b000) EAX <= alu_out; else EAX<=EAX;
|
61 |
|
|
if (dest==3'b001) ECX <= alu_out; else ECX<=ECX;
|
62 |
|
|
if (dest==3'b010) EDX <= alu_out; else EDX<=EDX;
|
63 |
|
|
if (dest==3'b011) EBX <= alu_out; else EBX<=EBX;
|
64 |
|
|
if (dest==3'b100) ESP <= alu_out; else ESP<=ESP;
|
65 |
|
|
if (dest==3'b101) EBP <= alu_out; else EBP<=EBP;
|
66 |
|
|
end
|
67 |
3 |
ultro |
else if (state==`mul)
|
68 |
|
|
begin
|
69 |
|
|
EAX <= {EAX[30:0],1'b0};
|
70 |
4 |
ultro |
if (EDX[0] == 1'b1) EBX <= EAX+EBX; else EBX <= EBX;
|
71 |
3 |
ultro |
EDX <= {1'b0,EDX[31:1]};
|
72 |
|
|
ECX <= ECX; ESP <= ESP; EBP <= EBP;
|
73 |
|
|
end
|
74 |
|
|
else if (state==`mul2)
|
75 |
|
|
begin
|
76 |
|
|
EAX <= EBX; EBX <= EBX; EDX <= EDX;
|
77 |
|
|
ECX <= ECX; ESP <= ESP; EBP <= EBP;
|
78 |
|
|
end
|
79 |
2 |
ultro |
else
|
80 |
3 |
ultro |
begin
|
81 |
|
|
EBP<=EBP; EAX<=EAX;ECX<=ECX; EDX<=EDX;
|
82 |
2 |
ultro |
case(state)
|
83 |
|
|
`jmp , `jg, `jge , `jl, `jle, `je, `jne, `imm, `call,
|
84 |
|
|
`lea : EBX<={EBX[31:16],ID[7:0],ID[15:8]};
|
85 |
|
|
`imm2 : EBX<={ID[7:0],ID[15:8], EBX[15:0]};
|
86 |
|
|
`lea2 : EBX<={ID[7:0],ID[15:8], EBX[15:0]}+EBP;
|
87 |
|
|
default : EBX<=EBX;
|
88 |
|
|
endcase
|
89 |
|
|
case(state)
|
90 |
|
|
`call : ESP<=ESP - 4'b0100;
|
91 |
|
|
`ret2 : ESP<=ESP + 4'b0100;
|
92 |
|
|
default: ESP<=ESP;
|
93 |
|
|
endcase
|
94 |
|
|
end
|
95 |
|
|
case(state)
|
96 |
|
|
`jge2 : PC<=pc_jge;
|
97 |
|
|
`jle2 : PC<=pc_jle;
|
98 |
|
|
`jg2 : PC<=pc_jg ;
|
99 |
|
|
`jl2 : PC<=pc_jl ;
|
100 |
|
|
`je2 : PC<=pc_eq ;
|
101 |
|
|
`jne2 : PC<=pc_neq;
|
102 |
|
|
`jmp2,`call2 : PC<=pc_jp ;
|
103 |
|
|
`ret2 : PC<=D ;
|
104 |
3 |
ultro |
`mul,`mul2 : PC<=PC ;
|
105 |
2 |
ultro |
default : PC<=incPC ;
|
106 |
|
|
endcase
|
107 |
|
|
end
|
108 |
|
|
// muxing for source selection, used in alu & moves
|
109 |
|
|
always@(src,EAX,ECX,EDX,EBX,ESP,EBP,D)
|
110 |
|
|
case(src)
|
111 |
|
|
3'b000 : regsrc = EAX;
|
112 |
|
|
3'b001 : regsrc = ECX;
|
113 |
|
|
3'b010 : regsrc = EDX;
|
114 |
|
|
3'b011 : regsrc = EBX;
|
115 |
|
|
3'b100 : regsrc = ESP;
|
116 |
|
|
3'b101 : regsrc = EBP;
|
117 |
|
|
3'b111 : regsrc = D;
|
118 |
|
|
default: regsrc = EBX;
|
119 |
|
|
endcase
|
120 |
|
|
// muxing for 2nd operand selection, used in alu only
|
121 |
|
|
always@(dest,EAX,ECX,EDX,EBX,ESP,EBP,D)
|
122 |
|
|
case(dest)
|
123 |
|
|
3'b000 : regdest = EAX;
|
124 |
|
|
3'b001 : regdest = ECX;
|
125 |
|
|
3'b010 : regdest = EDX;
|
126 |
|
|
3'b011 : regdest = EBX;
|
127 |
|
|
3'b100 : regdest = ESP;
|
128 |
|
|
3'b101 : regdest = EBP;
|
129 |
|
|
3'b111 : regdest = D ;
|
130 |
|
|
default: regdest = EBX;
|
131 |
|
|
endcase
|
132 |
|
|
// alu
|
133 |
4 |
ultro |
always@(state,regdest,regsrc,ID,cry,Zregsrc,Sregsrc,sft_out,adder_out,sub_out)
|
134 |
|
|
if (state == `fetch )
|
135 |
2 |
ultro |
case (ID[15:10])
|
136 |
4 |
ultro |
6'b000000 : {ncry,alu_out} = adder_out ; // ADD , carry generation
|
137 |
|
|
6'b000010 : {ncry,alu_out} = {cry,regdest | regsrc}; // OR
|
138 |
|
|
6'b000100 : {ncry,alu_out} = adder_out ; // ADD , carry use
|
139 |
|
|
6'b000110 : {ncry,alu_out} = sub_out ; // SUB , carry use
|
140 |
|
|
6'b001000 : {ncry,alu_out} = {cry,regdest & regsrc}; // AND
|
141 |
|
|
6'b001010 : {ncry,alu_out} = sub_out ; // SUB , carry generation
|
142 |
|
|
6'b001100 : {ncry,alu_out} = {cry,regdest ^ regsrc}; // XOR
|
143 |
|
|
6'b100010 : {ncry,alu_out} = {cry, regsrc}; // MOVE
|
144 |
|
|
6'b101101 : {ncry,alu_out} = {cry, Zregsrc}; // MOVE
|
145 |
|
|
6'b101111 : {ncry,alu_out} = {cry, Sregsrc}; // MOVE
|
146 |
|
|
6'b110000 : {ncry,alu_out} = {cry, sft_out[31:0]}; // SHIFT
|
147 |
|
|
6'b110100 : {ncry,alu_out} = {cry, sft_out[31:0]}; // SHIFT
|
148 |
|
|
default : {ncry,alu_out} = {cry,regdest }; // DO NOTHING
|
149 |
2 |
ultro |
endcase
|
150 |
4 |
ultro |
else {ncry,alu_out} = {cry,regdest };
|
151 |
2 |
ultro |
// Main instruction decode
|
152 |
3 |
ultro |
always @(ID,state,EDX)
|
153 |
2 |
ultro |
begin
|
154 |
|
|
// One cycle instructions, operand selection
|
155 |
|
|
if (state == `fetch)
|
156 |
|
|
begin
|
157 |
|
|
case ({ID[15:14],ID[9],ID[7]})
|
158 |
|
|
4'b1000 : begin src=ID[5:3]; dest= 3'b111; end // store into ram (x89 x00)
|
159 |
|
|
4'b1010 : begin src= 3'b111; dest=ID[5:3]; end // load from ram (x8b x00)
|
160 |
|
|
4'b1001 : begin src=ID[5:3]; dest=ID[2:0]; end // reg2reg xfer (x89 xC0)
|
161 |
3 |
ultro |
4'b1011 : begin src=ID[2:0]; dest=ID[5:3]; end // reg2reg xfer (x8b xC0)
|
162 |
2 |
ultro |
4'b0001 : begin src=ID[5:3]; dest=ID[2:0]; end // alu op
|
163 |
|
|
4'b0011 : begin src=ID[2:0]; dest=ID[5:3]; end // alu op
|
164 |
|
|
default : begin src=ID[5:3]; dest=ID[2:0]; end // shift
|
165 |
|
|
endcase
|
166 |
|
|
end
|
167 |
|
|
else if (state==`ret)
|
168 |
|
|
begin src = 3'b011; dest = 3'b100; end
|
169 |
|
|
else begin src = 3'b000; dest = 3'b000; end
|
170 |
|
|
// instructions that require more than one cycle to execute
|
171 |
|
|
if (state == `fetch)
|
172 |
|
|
begin
|
173 |
|
|
casex(ID)
|
174 |
|
|
16'h90e9: nstate = `jmp;
|
175 |
|
|
16'h0f8f: nstate = `jg;
|
176 |
|
|
16'h0f8e: nstate = `jle;
|
177 |
|
|
16'h0f8d: nstate = `jge;
|
178 |
|
|
16'h0f8c: nstate = `jl;
|
179 |
|
|
16'h0f85: nstate = `jne;
|
180 |
|
|
16'h0f84: nstate = `je;
|
181 |
|
|
16'h90bb: nstate = `imm;
|
182 |
|
|
16'h8d9d: nstate = `lea;
|
183 |
|
|
16'h90e8: nstate = `call;
|
184 |
|
|
16'h90c3: nstate = `ret;
|
185 |
3 |
ultro |
16'hc1xx: nstate = `shift;
|
186 |
|
|
16'hafc2: nstate = `mul;
|
187 |
2 |
ultro |
default : nstate = `fetch;
|
188 |
|
|
endcase
|
189 |
|
|
if (ID == 16'h9066) nprefx = 1'b1; else nprefx = 1'b0;
|
190 |
|
|
if (ID[15:8] == 8'h39 ) cmpr = 1'b1; else cmpr = 1'b0;
|
191 |
|
|
end
|
192 |
|
|
else
|
193 |
|
|
begin
|
194 |
3 |
ultro |
nprefx = 1'b0; cmpr = 1'b0;
|
195 |
|
|
if((state==`mul)&&!(EDX==32'b0)) nstate=`mul;
|
196 |
|
|
else if((state==`mul)&& (EDX==32'b0)) nstate=`mul2;
|
197 |
|
|
else if (state==`mul2) nstate = `fetch;
|
198 |
|
|
else if (state==`jmp) nstate = `jmp2; else if (state==`jmp2) nstate = `fetch;
|
199 |
2 |
ultro |
else if (state==`jne) nstate = `jne2; else if (state==`jne2) nstate = `fetch;
|
200 |
|
|
else if (state==`je ) nstate = `je2 ; else if (state==`je2 ) nstate = `fetch;
|
201 |
|
|
else if (state==`jge) nstate = `jge2; else if (state==`jge2) nstate = `fetch;
|
202 |
|
|
else if (state==`jg ) nstate = `jg2 ; else if (state==`jg2 ) nstate = `fetch;
|
203 |
|
|
else if (state==`jle) nstate = `jle2; else if (state==`jle2) nstate = `fetch;
|
204 |
|
|
else if (state==`jl ) nstate = `jl2 ; else if (state==`jl2 ) nstate = `fetch;
|
205 |
|
|
else if (state==`imm) nstate = `imm2; else if (state==`imm2) nstate = `fetch;
|
206 |
|
|
else if (state==`lea) nstate = `lea2; else if (state==`lea2) nstate = `fetch;
|
207 |
|
|
else if (state==`call) nstate = `call2; else if (state==`call2) nstate = `fetch;
|
208 |
|
|
else if (state==`ret) nstate = `ret2; else if (state==`ret2) nstate = `fetch;
|
209 |
|
|
else if (state==`shift) nstate = `fetch;
|
210 |
|
|
else nstate = `fetch;
|
211 |
|
|
end
|
212 |
|
|
end
|
213 |
|
|
assign IA = PC ;
|
214 |
|
|
assign sft_in = regdest ;
|
215 |
3 |
ultro |
assign A = (state == `call2) ? ESP : EBX ;
|
216 |
2 |
ultro |
assign shtr = ID[12] ? ECX[4:0] : EBX[4:0] ;
|
217 |
|
|
assign Q = (state == `call2) ? incPC : regsrc ;
|
218 |
|
|
assign WEN = (ID[15:8]==8'h90) ? 1'b1 :
|
219 |
|
|
(state == `call2) ? 1'b0 :
|
220 |
4 |
ultro |
(dest == 3'b111) ? 1'b0 : 1'b1 ;
|
221 |
2 |
ultro |
assign tst = sft_in >>> (shtr);
|
222 |
|
|
assign sft_out = (src == 3'b111) ? tst : //sar
|
223 |
|
|
(src == 3'b101) ? (sft_in >> shtr ) : //shr
|
224 |
|
|
(sft_in << shtr ) ; //shl
|
225 |
|
|
assign Sregsrc = ID[8] ? { {16{regsrc[15]}} , regsrc[15:0] } :
|
226 |
|
|
{ {24{regsrc[7] }} , regsrc[7:0] } ;
|
227 |
|
|
assign Zregsrc = ID[8] ? { 16'b0 , regsrc[15:0] } :
|
228 |
|
|
{ 24'b0 , regsrc[7:0] } ;
|
229 |
|
|
assign BEN = (state == `call2 ) ? 1'b1 :
|
230 |
|
|
{ prefx , ID[8] } ;
|
231 |
|
|
assign neqF = (regsrc == regdest) ? 1'b1 : 1'b0;
|
232 |
|
|
assign nlF = (regsrc > regdest) ? 1'b1 : 1'b0;
|
233 |
|
|
assign ngF = (regsrc < regdest) ? 1'b1 : 1'b0;
|
234 |
|
|
assign incPC = PC + 3'b010;
|
235 |
|
|
assign pc_jge = ( eqF|gF) ? pc_jp : incPC;
|
236 |
|
|
assign pc_jle = ( eqF|lF) ? pc_jp : incPC;
|
237 |
|
|
assign pc_jg = ( gF ) ? pc_jp : incPC;
|
238 |
|
|
assign pc_jl = ( lF ) ? pc_jp : incPC;
|
239 |
|
|
assign pc_eq = ( eqF ) ? pc_jp : incPC;
|
240 |
|
|
assign pc_neq = ( eqF ) ? incPC : pc_jp;
|
241 |
|
|
assign pc_jp = incPC+{ID,EBX[15:0]};
|
242 |
|
|
assign adder_out= nncry + regsrc + regdest;
|
243 |
|
|
assign sub_out= regdest - regsrc - nncry;
|
244 |
4 |
ultro |
assign nncry = (ID[12] ? cry : 1'b0);
|
245 |
2 |
ultro |
endmodule
|