当前位置: 首页 > news >正文

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

ALU模块代码如下:

// 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 inactiveinput 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 = EXECUTEif (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 instructioncase (decoded_alu_arithmetic_mux)ADD: begin alu_out_reg <= rs + rt;endSUB: begin alu_out_reg <= rs - rt;endMUL: begin alu_out_reg <= rs * rt;endDIV: begin alu_out_reg <= rs / rt;endendcaseendendendend
endmodule

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

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

    localparam IDLE = 3'b000, // Waiting to startFETCH = 3'b001,       // Fetch instructions from program memoryDECODE = 3'b010,      // Decode instructions into control signalsREQUEST = 3'b011,     // Request data from registers or memoryWAIT = 3'b100,        // Wait for response from memory if necessaryEXECUTE = 3'b101,     // Execute ALU and PC calculationsUPDATE = 3'b110,      // Update registers, NZP, and PCDONE = 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 。

http://www.dtcms.com/a/537173.html

相关文章:

  • 学做网站论坛vip码锦州宝地建设集团有限公司网站
  • Android15增强型视觉系统(EVS)
  • RK Android14 去除Setting apk顶部标题栏和搜索栏
  • K8s学习笔记(二十三) 网络策略 NetworkPolicy
  • 部门子网站建设方案公司名称大全简单
  • 关于JVM调优,我想聊聊数据和耐心
  • LED显示的一种思路---摄像机思想(学习总结)
  • 2021/12 JLPT听力原文 问题四
  • 算法入门:专题攻克二---滑动窗口(将x减到0的最小操作数,更新中。。。)
  • 如何知道网站是否被k找能做网站的
  • __金仓数据库平替MongoDB实战:医疗手术场景下的国产化替代方案__
  • 深圳有什么网站在html中做网站 视频
  • 比价网站源码整站程序河北省住房城乡建设局网站首页
  • 【从零开始开发远程桌面连接控制工具】02-远程控制服务端实现详解
  • 硬件电路LRC串联谐振分析
  • 做网站卖赚钱吗哪里有免费的网站域名
  • redis主从集群及其原理(优化)
  • 2025年10月27日 AI大事件
  • Logstash 多 Pipeline 配置
  • 网站建设需要注意事项深圳龙华医院网站建设
  • 云栖实录 | 实时计算 Flink 全新升级 - 全栈流处理平台助力实时智能
  • 网页建站怎么设置如何在电脑上建网站
  • 马上飞做的一些网站汝州市文明建设网站
  • 集群环境安装与部署 Hadoop
  • 【乱七八糟】【1. fs.inotify.max_user_watches 参数】
  • C++运算符重载与友元函数:理解输入输出流的魔法
  • Android Camera 从应用到硬件之- 枚举Camera - 1
  • 【Frida Android】基础篇13:Frida-Trace 基础简介——从命令到脚本的动态追踪入门
  • 使用electron-vite生成一个桌面应用以及引入必要插件
  • 龙岗网站设计机构网络培训平台建设方案