Get sim working, make some changes to the final addition

This commit is contained in:
Byron Lathi
2025-06-28 20:34:46 -07:00
parent 8136a7526b
commit 20d98e117b
8 changed files with 122 additions and 10 deletions

View File

@@ -3,6 +3,7 @@ module chacha20_block #(
parameter COUNTER_SIZE = 64,
parameter NONCE_SIZE = 64,
parameter STATE_SIZE = 512,
parameter ROUNDS = 20,
parameter CONSTANT = 128'h657870616e642033322d62797465206b
)(
input logic i_clk,
@@ -41,16 +42,19 @@ chacha20_qr u_chacha20_``name ( \
)
logic [31:0] state [21][16];
logic [3:0] valid[21];
logic [31:0] state [ROUNDS+1][16];
logic [3:0] valid[ROUNDS+1];
// logic [3:0] ready[21];
// small fifo for storing the initial state.
// better to store it in a memory than in flops
logic [4:0] initial_state_wptr;
logic [4:0] initial_state_rptr;
logic [511:0] initial_states [20];
logic [$clog2(ROUNDS)-1:0] initial_state_wptr;
logic [$clog2(ROUNDS)-1:0] initial_state_rptr;
logic [511:0] initial_states [ROUNDS];
logic [511:0] state_pre_add;
logic pre_add_valid;
logic [511:0] write_initial_state, read_initial_state;
@@ -65,14 +69,23 @@ always_ff @(posedge i_clk) begin
initial_states[initial_state_wptr] <= write_initial_state;
end
if (valid[19][0]) begin
pre_add_valid <= valid[ROUNDS][0];
if (valid[ROUNDS][0]) begin
read_initial_state <= initial_states[initial_state_rptr];
for (int i = 0; i < 16; i++) begin
state_pre_add[i*32 +: 32] <= state[ROUNDS][i];
end
end
o_valid <= &valid[20];
o_valid <= pre_add_valid;
// We cannot just add state_pre_add and read_initial state
// because the addition needs to be done wordwise, with no
// carries between 32 bit groups.
for (int i = 0; i < 16; i++) begin
o_state[i*32 +: 32] <= state[20][i] + read_initial_state[i*32 +: 32];
o_state[i*32 +: 32] <= state_pre_add[i*32 +: 32] + read_initial_state[i*32 +: 32];
end
end
@@ -111,7 +124,7 @@ end
generate
for (genvar round = 0; round < 20; round+=2) begin : ROUND_LOOP
for (genvar round = 0; round < ROUNDS; round+=2) begin : ROUND_LOOP
`QR(0, round, 0, 0, 4, 8, 12);
`QR(1, round, 1, 1, 5, 9, 13);
`QR(2, round, 2, 2, 6, 10, 14);