Ahmed Benlafqih

All projects

Extending an LC-3 processor in Logisim with bit-scan instructions

Academic project · 2024–2025

A computer-architecture coursework: completing a provided 16-bit LC-3 processor in Logisim with two bit-scan instructions, write-back selection and flag logic.

Source code

In short

What it is
Work inside a course-provided 16-bit LC-3 processor built in Logisim: I added two bit-scan instructions to the ALU, the logic that selects what is written back to a register, and the condition flags used by branches.
Why
A computer-architecture course, to build real datapath and control logic on top of a provided processor frame, under constraints on gate count and circuit depth.
My role
Coursework. I worked on the bit-scan block, the write-back selection and the flag logic, not the whole processor.
Domains and tech
Systems programming. Digital logic design, computer architecture, Logisim 2.7.1.
Status
Academic project. Completed coursework. The report records that the test programs gave the expected results; no run output is kept in the repository (see Limitations).
Period
2024 – 2025

Context and problem

This was a computer-architecture course project at EIDD (Université Paris Cité), second year, 2024–2025. The starting point was a 16-bit LC-3 processor built in Logisim and provided by the course, with some subcircuits left to complete (they carry “à compléter” labels) and a trace block marked “ne pas modifier” (do not modify). On top of that frame, I added two bit-scan instructions to the ALU, the logic that chooses what is written back to a register, and the condition-flag logic that branches read. A constraint of the assignment was to keep the gate count and the circuit depth low.

Report and README; the “à compléter” / “ne pas modifier” labels are in the circuit files.

Architecture

A Logisim schematic of the full LC-3 processor: blocks for the program counter, a 64 KB RAM, the instruction register, the decoder, the ALU, the register bank, write-back and flag logic, wired together, with a trace block of probes below.
The whole processor. The overall wiring and the trace block at the bottom (marked “do not modify”) are part of the course frame. My work is the ALU’s bit-scan operation, the write-back selector (WriteVal) and the flag logic (NZP).Logisim export, main circuit

The processor fetches an instruction, decodes it, runs it through the ALU or a memory access, and writes a result back to one of eight 16-bit registers. The parts I worked on are the ALU’s new operation, the write-back selector and the flag logic.

The two bit-scan instructions

Both instructions take a 16-bit register and return the index of the first bit set to 1; they differ in the direction of the scan. BSF (bit scan forward) scans from bit 0 upward, and BSB (bit scan backward) from bit 15 downward. For the input 1000 0000 0000 0010, BSF returns 1 and BSB returns 15.

Behaviour as described in the report.
A Logisim schematic of the ALU: three 16-bit inputs, a multiplexer selecting a constant or a register operand, an adder, an AND gate, a NOT gate, a bit-scan block, and a four-way output multiplexer selected by a two-bit signal, with a note that the selector must be 11 for the bit-scan operation.
The ALU. The bit-scan block is wired in as a fourth operation beside add, AND and NOT; a two-bit selector chooses between them (the note reads “e2 must be 11 for BSB/BSF”).Logisim export, ALU circuit

Inside the ALU, the bit-scan operation is built from a bit reverser, multiplexers, a 16-input priority encoder, a bit extender and a subtractor. The priority encoder finds the first set bit; reversing the input first lets the same encoder scan from the other end, and a mode bit selects the direction. The exact arithmetic that turns the encoder’s index back into a position is read from the wiring and was not simulated here, so I describe the building blocks rather than assert the precise encoding.

Write-back and flags

Two more pieces complete an instruction:

  • Write-back selection chooses what reaches the destination register: the ALU result, a value loaded from memory, or a program-counter-relative address for LEA, decoded from the instruction’s opcode.
  • Flag logic compares the result against zero to produce the negative / zero / positive condition, holds it, and exposes the signal that branches test. In this circuit that signal is the branch condition itself; whether the flags are updated is gated separately by the write-enable.

Limitations

  • No recorded test output. The report states that three test programs (for BSF, BSB, and LD with LEA) produced the expected register values, but no trace, screenshot or memory image of those runs is in the repository, and the three programs themselves aren’t included. So the result rests on the report’s word, not on evidence I can show.
  • The bit-scan arithmetic is read, not run. The direction and the first-set-bit behaviour come from the report; the precise index arithmetic is my reading of the wiring, not a simulated result.
  • A provided frame. Several subcircuits (the decoder, the register bank, the program counter) are identical to the starting frame, so they are not my work; the case study claims only the bit-scan block, the write-back selection and the flag logic.
  • The repository also holds a set of LC-3 assembly routines that the project report doesn’t mention; some don’t do what their comments say, so they aren’t presented here.

Possible next steps

Export the schematics of the bit-scan block and the flag logic (the parts that differ most from the frame), add the test programs with their memory images and observed register values, and reduce the circuit depth, which the report lists as the main thing to improve.