summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorvin <git@vineetk.net>2025-03-20 16:30:16 -0400
committervin <git@vineetk.net>2025-03-24 16:23:47 -0400
commitc5ccd5c22f22cbbc265555559c7eefd06428398b (patch)
treee3b88d3a4c457ca3423ac0f6d3170c6773027e91
parent6927c5bdc6009b2acb05a2a7d176003447340a34 (diff)
add shift instructions (sll, srl, sra, and immediates)
-rw-r--r--riscvsingle.sv50
1 files changed, 28 insertions, 22 deletions
diff --git a/riscvsingle.sv b/riscvsingle.sv
index f5933ce..45e4855 100644
--- a/riscvsingle.sv
+++ b/riscvsingle.sv
@@ -198,7 +198,7 @@ module riscvsingle(input logic clk, reset,
198 198
199 logic ALUSrc, RegWrite, Jump, Zero; 199 logic ALUSrc, RegWrite, Jump, Zero;
200 logic [1:0] ResultSrc, ImmSrc; 200 logic [1:0] ResultSrc, ImmSrc;
201 logic [2:0] ALUControl; 201 logic [3:0] ALUControl;
202 202
203 controller c(Instr[6:0], Instr[14:12], Instr[30], Zero, 203 controller c(Instr[6:0], Instr[14:12], Instr[30], Zero,
204 ResultSrc, MemWrite, PCSrc, 204 ResultSrc, MemWrite, PCSrc,
@@ -220,7 +220,7 @@ module controller(input logic [6:0] op,
220 output logic PCSrc, ALUSrc, 220 output logic PCSrc, ALUSrc,
221 output logic RegWrite, Jump, 221 output logic RegWrite, Jump,
222 output logic [1:0] ImmSrc, 222 output logic [1:0] ImmSrc,
223 output logic [2:0] ALUControl); 223 output logic [3:0] ALUControl);
224 224
225 logic [1:0] ALUOp; 225 logic [1:0] ALUOp;
226 logic Branch; 226 logic Branch;
@@ -262,25 +262,30 @@ module aludec(input logic opb5,
262 input logic [2:0] funct3, 262 input logic [2:0] funct3,
263 input logic funct7b5, 263 input logic funct7b5,
264 input logic [1:0] ALUOp, 264 input logic [1:0] ALUOp,
265 output logic [2:0] ALUControl); 265 output logic [3:0] ALUControl);
266 266
267 logic RtypeSub; 267 logic RtypeSub;
268 assign RtypeSub = funct7b5 & opb5; // TRUE for R-type subtract instruction 268 assign RtypeSub = funct7b5 & opb5; // TRUE for R-type subtract instruction
269 269
270 always_comb 270 always_comb
271 case(ALUOp) 271 case(ALUOp)
272 2'b00: ALUControl = 3'b000; // addition 272 2'b00: ALUControl = 4'b0000; // addition
273 2'b01: ALUControl = 3'b001; // subtraction 273 2'b01: ALUControl = 4'b0001; // subtraction
274 default: case(funct3) // R-type or I-type ALU 274 default: case(funct3) // R-type or I-type ALU
275 3'b000: if (RtypeSub) 275 3'b000: if (RtypeSub)
276 ALUControl = 3'b001; // sub 276 ALUControl = 4'b0001; // sub
277 else 277 else
278 ALUControl = 3'b000; // add, addi 278 ALUControl = 4'b0000; // add, addi
279 3'b010: ALUControl = 3'b101; // slt, slti 279 3'b001: ALUControl = 4'b0110; // sll, slli
280 3'b100: ALUControl = 3'b100; // xor, xori 280 3'b010: ALUControl = 4'b0101; // slt, slti
281 3'b110: ALUControl = 3'b011; // or, ori 281 3'b100: ALUControl = 4'b0100; // xor, xori
282 3'b111: ALUControl = 3'b010; // and, andi 282 3'b101: if (RtypeSub)
283 default: ALUControl = 3'bxxx; // ??? 283 ALUControl = 4'b1000; // sra, srai
284 else
285 ALUControl = 4'b0111; // srl, srli
286 3'b110: ALUControl = 4'b0011; // or, ori
287 3'b111: ALUControl = 4'b0010; // and, andi
288 default: ALUControl = 4'bxxxx; // ???
284 endcase 289 endcase
285 endcase 290 endcase
286endmodule 291endmodule
@@ -290,7 +295,7 @@ module datapath(input logic clk, reset,
290 input logic PCSrc, ALUSrc, 295 input logic PCSrc, ALUSrc,
291 input logic RegWrite, 296 input logic RegWrite,
292 input logic [1:0] ImmSrc, 297 input logic [1:0] ImmSrc,
293 input logic [2:0] ALUControl, 298 input logic [3:0] ALUControl,
294 output logic Zero, 299 output logic Zero,
295 output logic [31:0] PC, 300 output logic [31:0] PC,
296 input logic [31:0] Instr, 301 input logic [31:0] Instr,
@@ -361,7 +366,7 @@ module mux3 #(parameter WIDTH = 8)
361endmodule 366endmodule
362 367
363module alu(input logic [31:0] a, b, 368module alu(input logic [31:0] a, b,
364 input logic [2:0] alucontrol, 369 input logic [3:0] alucontrol,
365 output logic [31:0] result, 370 output logic [31:0] result,
366 output logic zero); 371 output logic zero);
367 372
@@ -376,14 +381,15 @@ module alu(input logic [31:0] a, b,
376 381
377 always_comb 382 always_comb
378 case (alucontrol) 383 case (alucontrol)
379 3'b000: result = sum; // add 384 4'b0000: result = sum; // add
380 3'b001: result = sum; // subtract 385 4'b0001: result = sum; // subtract
381 3'b010: result = a & b; // and 386 4'b0010: result = a & b; // and
382 3'b011: result = a | b; // or 387 4'b0011: result = a | b; // or
383 3'b100: result = a ^ b; // xor 388 4'b0100: result = a ^ b; // xor
384 3'b101: result = sum[31] ^ v; // slt 389 4'b0101: result = sum[31] ^ v; // slt
385 3'b110: result = a << b[4:0]; // sll 390 4'b0110: result = a << b[4:0]; // sll
386 3'b111: result = a >> b[4:0]; // srl 391 4'b0111: result = a >> b[4:0]; // srl
392 4'b1000: result = $signed(a) >>> b[4:0]; // srl
387 default: result = 32'bx; 393 default: result = 32'bx;
388 endcase 394 endcase
389 395