Skip to content

Refactor ALU to use SystemVerilog enums and fix branch logic#200

Merged
TheDeepestSpace merged 6 commits intoUTOSS:mainfrom
RexPaster:alu-enumeration
Mar 22, 2026
Merged

Refactor ALU to use SystemVerilog enums and fix branch logic#200
TheDeepestSpace merged 6 commits intoUTOSS:mainfrom
RexPaster:alu-enumeration

Conversation

@RexPaster
Copy link
Copy Markdown
Contributor

@RexPaster RexPaster commented Mar 9, 2026

PR: Refactor ALU Decoder and Update Branch Comparison Logic

Closes #175

Summary

Refactor the ALUdecoder to use the alu_op_t_low enum instead of raw
4-bit control values and update branch comparison handling.

Changes

  • Converted ALU.v to a SystemVerilog file to make it compatible with enum.
  • Replaced hardcoded ALU control values with the corresponding enum
    values (ALUAdd, ALUSub, ALUSLT, etc.).
  • Updated branch decoding to use existing ALU comparison operations.
  • Implemented BGE and BGEU using inverted SLT / SLTU results
    instead of introducing new ALU operations.
  • Reformatted the ALUdecoder module for improved readability.

Notes

Branch comparisons now follow standard RISC-V semantics:

  • BLT / BLTU branch directly on the comparison result.
  • BGE / BGEU branch on the inverted result.

The inversion is handled in the control logic rather than the ALU to
keep the ALU implementation generic.

@RexPaster RexPaster marked this pull request as ready for review March 9, 2026 18:03
@TheDeepestSpace
Copy link
Copy Markdown
Member

Hey @RexPaster i'll try to figure out the best way to get the CI working for you this week since you are on a fork

Comment thread src/types.svh Outdated
Comment thread src/ControlFSM.sv
Comment on lines -245 to +261
if (alu_result == 32'b1) begin
pc_src = PC_SRC__JUMP;
PCUpdate = 1'b1;
end
else pc_src = PC_SRC__INCREMENT;
case (funct3)
3'b100, 3'b110: begin // BLT, BLTU: branch if rs1 < rs2
if (alu_result) begin // Direct SLT/SLTU result
pc_src = PC_SRC__JUMP;
PCUpdate = 1'b1;
end
else pc_src = PC_SRC__INCREMENT;
end
3'b101, 3'b111: begin // BGE, BGEU: branch if rs1 >= rs2 (invert SLT/SLTU)
if (!alu_result) begin // Invert SLT/SLTU result for >= comparison
pc_src = PC_SRC__JUMP;
PCUpdate = 1'b1;
end
else pc_src = PC_SRC__INCREMENT;
end
default: pc_src = PC_SRC__INCREMENT;
endcase
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Gonna need to triple-check this since I think we were passing the base ISA tests before for the branch instructions, but might have missed something.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK. Originally the ALUDECODER was calling ALU functions that didn't even exist in the ALU parameters.sv section. It's possible that these were defined directly in the ALU without being defined as parameters in Parameters.sv, I'll check later today.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and the original process before implementing this provided unknown codes to the ALU, triggering the DEFAULT case condition.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey sorry for radio silence there, i've been bit busy, getting back to this now.

So im pretty sure what's happening here is that you basically merged the ALU paths for SGE/SGEU with SLT/SLTU, and in order to differentiate between BGE/BGEU and BLT/BLTU you need add this condition at the control FSM level.

And that is probably the reason why is was working fine before -- since we differentiated SGE and SLT on ALU level, even though it was technically not necessary since RISC-V does not "define" the opposite of SLT on spec-level.

I think this is a good improvement on your part here, probably would take up a bit less space, though I haven't actually checked this.

@RexPaster
Copy link
Copy Markdown
Contributor Author

Hey @RexPaster i'll try to figure out the best way to get the CI working for you this week since you are on a fork

Perfect! Let me know.

@RexPaster
Copy link
Copy Markdown
Contributor Author

Hey @RexPaster i'll try to figure out the best way to get the CI working for you this week since you are on a fork

Perfect! Let me know.

Would it be better if I did it as a branch of this Repository?

@TheDeepestSpace
Copy link
Copy Markdown
Member

Hey @RexPaster i'll try to figure out the best way to get the CI working for you this week since you are on a fork

Perfect! Let me know.

Would it be better if I did it as a branch of this Repository?

Hey i changed up some of the ci settings so PRs from forks should be able to run CI no problem now; can you rebase your PR onto latest main or merge main into your PR?

…e used. Update respective file references. Adjust ALU logic to use enumerated type. TODO: integrate ENUM into ALU_decoder.sv
…LT/SLTU results | Implement enum in ALUdecoder.sv
@RexPaster
Copy link
Copy Markdown
Contributor Author

@TheDeepestSpace Done! I had to look up what a rebase is but I think i did it correctly.

@TheDeepestSpace
Copy link
Copy Markdown
Member

Nice! looks like you did it right, and CI is running too now. Lemme know if you can see the outputs/commands it runs (you can also see the command in ci.yaml)

@RexPaster
Copy link
Copy Markdown
Contributor Author

Give me a bit to fix these errors!

@TheDeepestSpace
Copy link
Copy Markdown
Member

No rush at all!

@RexPaster
Copy link
Copy Markdown
Contributor Author

RexPaster commented Mar 11, 2026

@TheDeepestSpace Lets try that!

@RexPaster
Copy link
Copy Markdown
Contributor Author

I forgot to implement the changes in src/utoss_riscv.sv to switch it to enum. Im doing it now!

@RexPaster
Copy link
Copy Markdown
Contributor Author

ok! theoretically this should work. It passes svlint and the docker build!

@RexPaster
Copy link
Copy Markdown
Contributor Author

Horray!

@TheDeepestSpace TheDeepestSpace merged commit 1c023cd into UTOSS:main Mar 22, 2026
8 checks passed
@TheDeepestSpace
Copy link
Copy Markdown
Member

Thanks for the PR @RexPaster!

@RexPaster RexPaster deleted the alu-enumeration branch March 22, 2026 20:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Convert ALU control into enum

2 participants