tiny-gpu入门4: ALU模块分析

ALU模块代码如下:

bash 复制代码
// ARITHMETIC-LOGIC UNIT
// > Executes computations on register values
// > In this minimal implementation, the ALU supports the 4 basic arithmetic operations
// > Each thread in each core has it's own ALU
// > ADD, SUB, MUL, DIV instructions are all executed here
module alu (
    input wire clk,
    input wire reset,
    input wire enable, // If current block has less threads then block size, some ALUs will be inactive

    input reg [2:0] core_state,

    input reg [1:0] decoded_alu_arithmetic_mux,
    input reg decoded_alu_output_mux,

    input reg [7:0] rs,
    input reg [7:0] rt,
    output wire [7:0] alu_out
);
    localparam ADD = 2'b00,
        SUB = 2'b01,
        MUL = 2'b10,
        DIV = 2'b11;

    reg [7:0] alu_out_reg;
    assign alu_out = alu_out_reg;

    always @(posedge clk) begin 
        if (reset) begin 
            alu_out_reg <= 8'b0;
        end else if (enable) begin
            // Calculate alu_out when core_state = EXECUTE
            if (core_state == 3'b101) begin 
                if (decoded_alu_output_mux == 1) begin 
                    // Set values to compare with NZP register in alu_out[2:0]
                    alu_out_reg <= {5'b0, (rs - rt > 0), (rs - rt == 0), (rs - rt < 0)};
                end else begin 
                    // Execute the specified arithmetic instruction
                    case (decoded_alu_arithmetic_mux)
                        ADD: begin 
                            alu_out_reg <= rs + rt;
                        end
                        SUB: begin 
                            alu_out_reg <= rs - rt;
                        end
                        MUL: begin 
                            alu_out_reg <= rs * rt;
                        end
                        DIV: begin 
                            alu_out_reg <= rs / rt;
                        end
                    endcase
                end
            end
        end
    end
endmodule

ALU会基于控制信号:[2:0] core_state、decoded_alu_output_mux和[1:0]decoded_alu_arithmetic_mux,对寄存器rs和rt的值执行具体的计算。

core_state为执行态(EXECUTE)的话开始执行具体的操作,所有状态如下:

bash 复制代码
    localparam IDLE = 3'b000, // Waiting to start
        FETCH = 3'b001,       // Fetch instructions from program memory
        DECODE = 3'b010,      // Decode instructions into control signals
        REQUEST = 3'b011,     // Request data from registers or memory
        WAIT = 3'b100,        // Wait for response from memory if necessary
        EXECUTE = 3'b101,     // Execute ALU and PC calculations
        UPDATE = 3'b110,      // Update registers, NZP, and PC
        DONE = 3'b111;        // Done executing this block

只有EXECUTE态才执行操作。

接下来有两个分支以及两个控制信号:decoded_alu_output_mux和[1:0]decoded_alu_arithmetic_mux,decoded_alu_output_mux用来控制ALU的操作,decoded_alu_arithmetic_mux控制执行的是具体的运算指令(即加减乘除)。

decoded_alu_output_mux为0则ALU会执行具体的运算指令,为1的话则会执行BRnzp指令,如果NZP寄存器符合指令中的' NZP '条件,则分支指令跳转到另一行程序内存,以这种方式来实现循环和条件指令的执行(CMP指令结果判定)。

最终结果输出到alu_out 。

相关推荐
小白狮ww17 小时前
Ovis-Image:卓越的图像生成模型
人工智能·深度学习·目标检测·机器学习·cpu·gpu·视觉分割模型
virtaitech2 天前
云平台一键部署【rednote-hilab/dots.ocr】多语言文档布局解析模型
人工智能·科技·ai·ocr·gpu·算力
virtaitech3 天前
如何评价趋动科技推出永久免费的OrionX社区版?
人工智能·科技·ai·免费·gpu·池化技术
lixzest4 天前
基于CPU开发或GPU开发的区别
gpu算力
minhuan5 天前
大模型应用:GPU的黑盒拆解:可视化看透大模型并行计算的底层逻辑.67
gpu算力·大模型应用·cuda原理·张量核心·显存解析
科学计算技术爱好者7 天前
NVIDIA GPU 系列用途分类梳理
人工智能·算法·gpu算力
飞鹰517 天前
CUDA高级优化实战:Stream、特殊内存与卷积优化—Week3学习总结
c++·gpt·chatgpt·gpu算力
骥龙8 天前
第一篇:背景篇 - 为什么医院需要自己的超算?
云计算·aigc·gpu算力
minhuan8 天前
大模型应用:拆解大模型算力需求:算力是什么?怎么衡量?如何匹配?.64
人工智能·gpu算力·大模型应用·算力评估·算力优化
virtaitech8 天前
云平台一键部署【Tencent-YouTu-Research/Youtu-LLM-2B】具备原生智能体能力
人工智能·深度学习·机器学习·ai·gpu·算力·云平台