Skip to content

Commit 1a586e0

Browse files
committed
accumulator fix
1 parent 5bc60f1 commit 1a586e0

File tree

5 files changed

+41
-33
lines changed

5 files changed

+41
-33
lines changed

src/cpu_core.v

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,10 @@ module cpu_core (
5353
shift_reg #(8) acc (
5454
.clk(clk),
5555
.rstn(rst_n),
56-
.en(acc_shift_en || acc_write_en),
57-
.load(acc_write_en),
58-
.dir(1'b1),
56+
.en(acc_write_en),
5957
.serial_in(alu_result),
6058
.parallel_in(8'b0),
61-
.q(out),
62-
.serial_out(acc_out_bit)
59+
.q(out)
6360
);
6461

6562
// ALU
@@ -91,6 +88,7 @@ module cpu_core (
9188
.btn_edge(btn_edge),
9289
.bit_done(bit_done),
9390
.alu_op(alu_op),
91+
.acc_write_en(acc_write_en),
9492
.reg_shift_en(reg_shift_en),
9593
.reg_write_en(reg_write_en),
9694
.acc_write_en(acc_write_en),
@@ -101,5 +99,5 @@ module cpu_core (
10199
.carry_en(carry_en)
102100
);
103101

104-
wire _unused = &{carry_in, acc_shift_en, 1'b0, reg_write_en, acc_write_en};
102+
wire _unused = &{carry_in, acc_shift_en, 1'b0, reg_write_en};
105103
endmodule

src/fsm_control.v

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ module fsm_control (
6262
next_state = S_EXECUTE;
6363

6464
S_EXECUTE:
65-
if (bit_done)
6665
next_state = S_WRITE_ACC;
6766

6867
S_WRITE_ACC:
@@ -97,9 +96,11 @@ module fsm_control (
9796
end
9897

9998
S_WRITE_ACC: begin
100-
acc_write_en = 1;
101-
acc_shift_en = 1;
99+
reg_shift_en = 1;
100+
alu_op = decode_alu_op(opcode);
102101
en_counter = 1;
102+
carry_en = 1;
103+
acc_write_en = 1;
103104
end
104105
endcase
105106
end

src/shift_reg.v

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// shift_reg.v - bidirectional shift register with optional parallel load
1+
// shift_reg.v - shift register with optional parallel load
22

33
`default_nettype none
44

@@ -9,27 +9,30 @@ module shift_reg
99
input wire rstn, // active-low synchronous reset
1010
input wire en, // 1 -> perform load/shift
1111
input wire load, // 1 -> parallel load, 0 -> shift
12-
input wire dir, // 0 -> shift left, 1 -> shift right
1312
input wire serial_in, // bit entering during a shift
1413
input wire [WIDTH-1:0] parallel_in, // data for parallel load
15-
output reg [WIDTH-1:0] q, // register contents
16-
output wire serial_out // bit exiting during a shift
14+
output reg [WIDTH-1:0] q // register contents
1715
);
1816

17+
reg [$clog2(WIDTH)-1:0] bit_index;
18+
1919
// next-state datapath
2020
wire [WIDTH-1:0] next_q =
21-
load ? parallel_in : // parallel load
22-
(dir == 1'b0) ? {q[WIDTH-2:0], serial_in} : // shift left
23-
{serial_in, q[WIDTH-1:1]}; // shift right
21+
load ? parallel_in : {serial_in, q[WIDTH-1:1]};
2422

25-
// sequential part
26-
always @(posedge clk)
27-
if (!rstn)
23+
always @(posedge clk or negedge rstn) begin
24+
if (!rstn) begin
2825
q <= {WIDTH{1'b0}}; // synchronous clear
29-
else if (en)
30-
q <= next_q; // load or shift
31-
32-
// bit shifted out on this cycle
33-
assign serial_out = dir ? q[0] : q[WIDTH-1];
26+
bit_index <= 0;
27+
end else if (en) begin
28+
if (load) begin
29+
q[WIDTH-1:0] <= parallel_in;
30+
bit_index <= 0;
31+
end else begin
32+
q[bit_index] <= serial_in;
33+
bit_index <= bit_index + 1; // increment bit index each cycle
34+
end
35+
end
36+
end
3437

3538
endmodule

test/test.gtkw

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
[*]
22
[*] GTKWave Analyzer v3.3.104 (w)1999-2020 BSI
3-
[*] Tue Jul 8 20:02:40 2025
3+
[*] Tue Jul 8 23:26:25 2025
44
[*]
55
[dumpfile] "/workspaces/bit-serial-CPU/test/tb.vcd"
6-
[dumpfile_mtime] "Tue Jul 8 19:58:49 2025"
7-
[dumpfile_size] 9847
6+
[dumpfile_mtime] "Tue Jul 8 23:25:01 2025"
7+
[dumpfile_size] 9954
88
[savefile] "/workspaces/bit-serial-CPU/test/test.gtkw"
9-
[timestart] 772800000
10-
[size] 1000 600
9+
[timestart] 737400000
10+
[size] 1182 600
1111
[pos] -1 -1
12-
*-26.000000 845200000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
12+
*-26.000000 912800000 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
1313
[treeopen] tb.
1414
[treeopen] tb.user_project.
1515
[treeopen] tb.user_project.u_cpu_core.
1616
[sst_width] 214
17-
[signals_width] 150
17+
[signals_width] 210
1818
[sst_expanded] 1
19-
[sst_vpaned_height] 60
19+
[sst_vpaned_height] 171
2020
@22
2121
tb.ui_in[7:0]
2222
tb.uio_in[7:0]
@@ -40,5 +40,11 @@ tb.user_project.u_cpu_core.regfile.rs1_bit
4040
tb.user_project.u_cpu_core.regfile.rs2_bit
4141
tb.user_project.u_cpu_core.regfile.rs1_addr[2:0]
4242
tb.user_project.u_cpu_core.regfile.rs2_addr[2:0]
43+
tb.user_project.u_cpu_core.alu.alu_op[1:0]
44+
tb.user_project.u_cpu_core.alu.result
45+
@23
46+
tb.user_project.u_cpu_core.acc.q[7:0]
47+
@28
48+
tb.user_project.u_cpu_core.acc.en
4349
[pattern_trace] 1
4450
[pattern_trace] 0

test/test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ async def test_project(dut):
6868
dut._log.info("Test project behavior")
6969

7070
# Input first 8 instruction bits
71-
await load_instruction(dut, 0x32)
71+
await load_instruction(dut, 0b00111100)
7272

7373
# Input second 8 instruction bits
7474
await load_instruction(dut, 0x04)

0 commit comments

Comments
 (0)