📄 decoder.v
字号:
// Operations: ADD, ADDU, SUB, SUBU, AND, OR, NOR, XOR, SLL, SRA, SRL
module decoder(Clock, Reset, Instruction, Branch, Jump, Write_RF, Src2, ALUOP, RegDest, Memory_Read, Memory_Write, Mem2Reg, IntFP, ArSh);
parameter InstWidth = 32;
// ---------------------------- Input ports ----------------------------
input Clock;
input Reset;
input [InstWidth-1:0] Instruction;
// ---------------------------- Output ports ----------------------------
output Branch;
output Jump;
output Write_RF;
output Src2;
output ALUOP;
output RegDest;
output Memory_Read;
output Memory_Write;
output Mem2Reg;
output IntFP;
output ArSh;
// ---------------------------- Type of Output ports ----------------------------
reg Branch;
reg Jump;
reg Write_RF;
reg Src2;
reg [5:0] ALUOP;
reg [1:0] RegDest;
reg Memory_Read;
reg Memory_Write;
reg [2:0] Mem2Reg;
reg IntFP;
reg ArSh;
// ----------------------------------------------------
// Code Starts Here
// ----------------------------------------------------
// Include the definition of all the ALU Operations
`include "Operations.v" // File "Operations.v" contains all operation codes
`include "OperationsFP.v" // File "OperationsFP.v" contains floating point opcodes
always @ (posedge Clock)
begin
if(Reset == 1)
begin
//If the reset is on, it will clear each output
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b0; //With 0 we don't write to the register file
Src2 = 1'b0; // With 0 the Mux selects Output B from RegFile
ALUOP = NOP; //No Operation Opcode
RegDest = 2'b00; // Doesn't matter
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // if (Reset == 1)
else
begin
case(Instruction[InstWidth-1:InstWidth-6])
NOP: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b0; //With 0 we don't write to the register file
Src2 = 1'b0; // With 0 the Mux selects Output B from RegFile
ALUOP = NOP; //No Operation Opcode
RegDest = 2'b00; // Doesn't matter
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: NOP
// ################ Arithmetic Immediate Operations ################
ADDI: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD; //ADDI = S_ADD Opcode
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: ADDI
ADDIU: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADDU; //ADDIU Opcode
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: ADDIU
ANDI: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_AND; //ANDI Opcode
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: ANDI
ORI: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_OR; //ORI Opcode
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: ORI
XORI: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_XOR; //XORI Opcode
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b000; //With 0 the Mux selects the output from the ALU
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: XORI
// ################ Load Operations ################
LB: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD; //LB Opcode (ALU Output = Immediate Num extended to 32)
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b1; //Reads from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b001; //With 1 the Mux selects the output from the memory after this gets sign extended (24 bits)
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: LB
LBU: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD; //LBU Opcode (ALU Output = Immediate Num extended to 32)
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b1; //Reads from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b010; //With 2 the Mux selects the output from the memory after this gets zero extended (24 bits)
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: LBU
LH: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD; //LH Opcode (ALU Output = Immediate Num extended to 32)
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b1; //Reads from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b011; //With 3 the Mux selects the output from the memory after this gets sign extended (16 bits)
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: LH
LHU: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD; //LHU Opcode (ALU Output = Immediate Num extended to 32)
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b1; //Reads from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b100; //With 4 the Mux selects the output from the memory after this gets zero extended (16 bits)
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: LHU
LUI: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD;
RegDest = 2'b00; // With 0, the Mux selects Rt
Memory_Read = 1'b0; //Doesn't read from memory
Memory_Write = 1'b0; //Doesn't write to memory
Mem2Reg = 3'b100; //With 4 the Mux selects the output from the memory after it gets zero extended
IntFP = 1'b0; //Select the ALU unit
ArSh = 1'b0; //SrcA = I[25-21]
end // case: LUI
LW: begin
Branch = 1'b0; //With 0, the PC is incremented in 1
Jump = 1'b0; //So that PC gets incremented in 1
Write_RF = 1'b1; //With 1 we write to the register file
Src2 = 1'b1; // With 1 the Mux selects the Immediate Value
ALUOP = S_ADD; //LBU Opcode (ALU Output = Immediate Num extended to 32)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -