The code below is very hard to read. Please rewrite it in Verilog, considering readability and clean code principles. You can modularize it into functions or increase the number of lines.
Certainly! I have modularized the given code into a Verilog module that improves readability by using multiple lines and adding comments. Here's the rewritten code:
verilog
User
Assistant
module AddressGenerator (
input [31:0] mat_a_addr_i,
input [31:0] mat_b_addr_i,
input [15:0] Aread_count,
input [15:0] mat_width_reg,
output reg [31:0] araddr
);
// Internal variables concatenation results
wire [31:0] mat_a_address;
wire [31:0] mat_b_address;
// Calculate the matrix A address
assign mat_a_address = mat_a_addr_i + {Aread_count, 6'b0};
// Calculate the matrix B address
assign mat_b_address = mat_b_addr_i + {(Aread_count - mat_width_reg), 6'b0};
// Determine the araddr based on Aread_count and mat_width_reg
always @(*) begin
if (Aread_count < mat_width_reg) begin
araddr = mat_a_address;
end else begin
araddr = mat_b_address;
end
end
endmodule
Explanation of Changes:
Modularization: The code is encapsulated in a Verilog module named AddressGenerator.
Internal Variables: Intermediate wires mat_a_address and mat_b_address are created to store concatenated results.
Computation Separation: The addresses for matrix A and matrix B are computed separately for clarity.
Conditional Assignment: The conditional logic to determine araddr is included in an always @(*) block for better readability and modularity.
Comments: Added comments to explain each step of the computation.
This approach improves readability and maintainability while retaining the original functionality.
araddr = (Aread_count < mat_width_reg) ? mat_a_addr_i + {Aread_count,6'b0} : mat_b_addr_i + {(Aread_count-mat_width_reg), 6'b0};
아래 코드를 참고해서 위의 코드를 깔끔하게 만들어줘. 스멜을 없애고 가독성이 좋게 작성해줘. 코드라인이 늘어나도 좋아.
localparam S_IDLE = 2'b00;
localparam S_PROCESS = 2'b01;
localparam W_ADDR = 2'b01;
localparam W_DATA = 2'b10;
localparam W_RESP = 2'b11;
always_ff @(posedge clk) begin
if (!rst_n) begin
ar_state <= S_IDLE;
read_state <= S_IDLE;
write_state <= S_IDLE;
read_count <= 7'd0;
W_count <= 4'd0;
Aread_count <= 3'd0;
mat_width_reg <= 3'd0;
end else begin
ar_state <= ar_state_n;
read_state <= read_state_n;
write_state <= write_state_n;
read_count <= read_count_n;
W_count <= write_count_n;
Aread_count <= Aread_count_n;
mat_width_reg <= mat_width_i[4:2];
end
end
task handle_ar_idle();
begin
ar_state_n = S_PROCESS;
arvalid = 1'b1;
araddr = mat_a_addr_i;
end
endtask
always_comb begin
set_defaults();
code
case(ar_state)
S_IDLE:begin
if (start_i) begin
handle_ar_idle();
end
end
S_PROCESS:begin
arvalid = 1'b1;
araddr = (Aread_count < mat_width_reg) ? mat_a_addr_i + {Aread_count,6'b0} : mat_b_addr_i + {(Aread_count-mat_width_reg), 6'b0};
if (axi_ar_if.arready) begin
if (Aread_count != {mat_width_reg,1'b0} - 1'd1) begin
Aread_count_n = Aread_count + 3'd1;
end
else begin
ar_state_n = S_IDLE;
Aread_count_n = 3'd0;
end
end
end
endcase
case(read_state)
S_IDLE:begin
if (axi_ar_if.arready) begin
read_state_n = S_PROCESS;
end
end
S_PROCESS:begin
rready = 1'b1;
if (axi_r_if.rvalid) begin
if (read_count[6:4] < mat_width_reg) begin
buf_a_wren_o = 1'b1;
end
else begin
buf_b_wren_o = 1'b1;
end
if ((read_count[6:4] == mat_width_i[4:1] - 1'd1) & axi_r_if.rlast) begin
read_count_n = 7'd0;
read_state_n = S_IDLE;
mm_start_o = 1'b1;
end
else begin
read_count_n = read_count + 7'd1;
end
end
end
endcase
case(write_state)
S_IDLE:begin
if (mm_start_o) begin
write_state_n = W_ADDR;
end
end
W_ADDR:begin
awvalid = mm_done_i;
if (axi_aw_if.awready) begin
write_state_n = W_DATA;
end
end
W_DATA:begin
wvalid = 1'b1;
if (W_count == 4'hF) begin
wlast = 1'b1;
end
if (axi_w_if.wready) begin
if (W_count != 4'hF) begin
write_count_n = W_count + 4'd1;
end
else begin
write_count_n = 4'd0;
write_state_n = W_RESP;
end
end
end
W_RESP:begin
if (axi_b_if.bvalid) begin
write_state_n = S_IDLE;
end
end
endcase