查看: 3929|回复: 0

[原创] mini risc mcu源码及测试

[复制链接]

该用户从未签到

发表于 2021-9-24 16:36:49 | 显示全部楼层 |阅读模式
分享到:
源码来自risc-v中文社区的这个帖子,本mini risc mcu学习源代码有二个对应的文件,一个是chisel源码文件,另一个是对应的verilog源文件,其中chisel源文件进行了行注释,相信不懂chisel的也能明白很多东西:还有一个帖子模拟如何烧写bin文件到 flash,上电boot,读指令并执行并判断结果
chisel源码:
import chisel3._
import chisel3.util._
class Risc extends Module{
  val io = IO(new Bundle {
    val isWr = Input(Bool())
    val wrAddr = Input(UInt(8.W))
    val wrData = Input(UInt(32.W))
    val boot = Input(Bool())
    val valid = Output(Bool())
    val out = Output(UInt(32.W))
  })
  val file = Mem(256,UInt(32.W)) //Mem的构造参数,第一个是数量,第二个是chisel类型 file表示二进制码文件.bin内容
  val code = Mem(256,UInt(32.W)) //指令代码区
  val pc = RegInit(0.U(8.W)) //当前指令地址指针
  val add_op :: imm_op :: Nil = Enum(2) //操作符
  val inst = code(pc) //根据pc值在代码区取指令值
  val op = inst(31,24) //32位指令码中高8位
  val rci = inst(23,16) //32位指令码中次高8位
  val rai = inst(15,8) //32位指令码中中8位 //相对偏移地址
  val rbi = inst(7,0) //32位指令码中低8位 //相对偏移地址
  val ra = Mux(rai === 0.U,0.U,file(rai)) //根据指令码中中8位地址从.bin文件中取对应偏移地址所对应的值
  val rb = Mux(rbi === 0.U,0.U,file(rbi)) //根据指令码中低8位地址从.bin文件中取对应偏移地址所对应的值
  val rc = Wire(UInt(32.W)) //指令操作的结果
  io.valid := false.B //默认值 因为io.valid在逻辑判断过程中是有条件改变值,所以根据语法需要,output类型需要赋初值,否则会报错
  io.out := 0.U
  rc := 0.U

  when(io.isWr) { //写
    code(io.wrAddr) := io.wrData  //将数据写到代码区
  } .elsewhen(io.boot) { //boot 上电启动时
    pc := 0.U  //当前指令地址要复位
  } .otherwise { //从代码区取指执行
    switch(op) { //根据指令码的高8位区分指令
      is(add_op) {  //add操作 间接寻址
        rc := ra + rb //最终io.out输出结果为指令码中低8位与中8位对应的.bin二进制文件中偏址内容结果之和
      }
      is(imm_op) { //imm操作 直接寻址
        rc := (rai << 8.U).asUInt() | rbi  //指令码中中8位作高8位,指令码中低8位作低8位,这样的16位作为最终输出
      }
    }
    io.out := rc
when(rci === 255.U) { //指令码中次高8位全为1
      io.valid := true.B
    } .otherwise {
      file(rci) := rc
}
    pc := pc + 1.U //指令地址指针加1
  }
}


verilog代码:
module Risc(
  input         clock,
  input         reset,
  input         io_isWr,
  input  [7:0]  io_wrAddr,
  input  [31:0] io_wrData,
  input         io_boot,
  output        io_valid,
  output [31:0] io_out
);
  reg [31:0] file [0:255]; // @[Risc.scala 14:17]
  reg [31:0] _RAND_0;
  wire [31:0] file__T_1_data; // @[Risc.scala 14:17]
  wire [7:0] file__T_1_addr; // @[Risc.scala 14:17]
  wire [31:0] file__T_3_data; // @[Risc.scala 14:17]
  wire [7:0] file__T_3_addr; // @[Risc.scala 14:17]
  wire [31:0] file__T_12_data; // @[Risc.scala 14:17]
  wire [7:0] file__T_12_addr; // @[Risc.scala 14:17]
  wire  file__T_12_mask; // @[Risc.scala 14:17]
  wire  file__T_12_en; // @[Risc.scala 14:17]
  reg [31:0] code [0:255]; // @[Risc.scala 15:17]
  reg [31:0] _RAND_1;
  wire [31:0] code_inst_data; // @[Risc.scala 15:17]
  wire [7:0] code_inst_addr; // @[Risc.scala 15:17]
  wire [31:0] code__T_4_data; // @[Risc.scala 15:17]
  wire [7:0] code__T_4_addr; // @[Risc.scala 15:17]
  wire  code__T_4_mask; // @[Risc.scala 15:17]
  wire  code__T_4_en; // @[Risc.scala 15:17]
  reg [7:0] pc; // @[Risc.scala 16:19]
  reg [31:0] _RAND_2;
  wire [7:0] op = code_inst_data[31:24]; // @[Risc.scala 19:16]
  wire [7:0] rci = code_inst_data[23:16]; // @[Risc.scala 20:17]
  wire [7:0] rai = code_inst_data[15:8]; // @[Risc.scala 21:17]
  wire [7:0] rbi = code_inst_data[7:0]; // @[Risc.scala 22:17]
  wire  _T = rai == 8'h0; // @[Risc.scala 23:20]
  wire [31:0] ra = _T ? 32'h0 : file__T_1_data; // @[Risc.scala 23:15]
  wire  _T_2 = rbi == 8'h0; // @[Risc.scala 24:20]
  wire [31:0] rb = _T_2 ? 32'h0 : file__T_3_data; // @[Risc.scala 24:15]
  wire  _T_5 = 8'h0 == op; // @[Conditional.scala 37:30]
  wire [31:0] _T_7 = ra + rb; // @[Risc.scala 37:18]
  wire  _T_8 = 8'h1 == op; // @[Conditional.scala 37:30]
  wire [15:0] _GEN_31 = {rai, 8'h0}; // @[Risc.scala 40:20]
  wire [22:0] _T_9 = {{7'd0}, _GEN_31}; // @[Risc.scala 40:20]
  wire [22:0] _GEN_32 = {{15'd0}, rbi}; // @[Risc.scala 40:37]
  wire [22:0] _T_10 = _T_9 | _GEN_32; // @[Risc.scala 40:37]
  wire [22:0] _GEN_0 = _T_8 ? _T_10 : 23'h0; // @[Conditional.scala 39:67]
  wire [31:0] _GEN_1 = _T_5 ? _T_7 : {{9'd0}, _GEN_0}; // @[Conditional.scala 40:58]
  wire  _T_11 = rci == 8'hff; // @[Risc.scala 44:14]
  wire  _GEN_5 = _T_11 ? 1'h0 : 1'h1; // @[Risc.scala 44:25]
  wire [31:0] _GEN_9 = io_boot ? 32'h0 : _GEN_1; // @[Risc.scala 32:24]
  wire [31:0] rc = io_isWr ? 32'h0 : _GEN_9; // @[Risc.scala 30:17]
  wire [7:0] _T_14 = pc + 8'h1; // @[Risc.scala 49:14]
  wire [31:0] _GEN_10 = io_boot ? 32'h0 : rc; // @[Risc.scala 32:24]
  wire  _GEN_11 = io_boot ? 1'h0 : _T_11; // @[Risc.scala 32:24]
  wire  _GEN_14 = io_boot ? 1'h0 : _GEN_5; // @[Risc.scala 32:24]
  assign file__T_1_addr = code_inst_data[15:8];
  assign file__T_1_data = file[file__T_1_addr]; // @[Risc.scala 14:17]
  assign file__T_3_addr = code_inst_data[7:0];
  assign file__T_3_data = file[file__T_3_addr]; // @[Risc.scala 14:17]
  assign file__T_12_data = io_isWr ? 32'h0 : _GEN_9;
  assign file__T_12_addr = code_inst_data[23:16];
  assign file__T_12_mask = 1'h1;
  assign file__T_12_en = io_isWr ? 1'h0 : _GEN_14;
  assign code_inst_addr = pc;
  assign code_inst_data = code[code_inst_addr]; // @[Risc.scala 15:17]
  assign code__T_4_data = io_wrData;
  assign code__T_4_addr = io_wrAddr;
  assign code__T_4_mask = 1'h1;
  assign code__T_4_en = io_isWr;
  assign io_valid = io_isWr ? 1'h0 : _GEN_11; // @[Risc.scala 26:12 Risc.scala 45:16]
  assign io_out = io_isWr ? 32'h0 : _GEN_10; // @[Risc.scala 27:10 Risc.scala 43:12]
`ifdef RANDOMIZE_GARBAGE_ASSIGN
`define RANDOMIZE
`endif
`ifdef RANDOMIZE_INVALID_ASSIGN
`define RANDOMIZE
`endif
`ifdef RANDOMIZE_REG_INIT
`define RANDOMIZE
`endif
`ifdef RANDOMIZE_MEM_INIT
`define RANDOMIZE
`endif
`ifndef RANDOM
`define RANDOM $random
`endif
`ifdef RANDOMIZE_MEM_INIT
  integer initvar;
`endif
`ifndef SYNTHESIS
initial begin
  `ifdef RANDOMIZE
    `ifdef INIT_RANDOM
      `INIT_RANDOM
    `endif
    `ifndef VERILATOR
      `ifdef RANDOMIZE_DELAY
        #`RANDOMIZE_DELAY begin end
      `else
        #0.002 begin end
      `endif
    `endif
  _RAND_0 = {1{`RANDOM}};
  `ifdef RANDOMIZE_MEM_INIT
  for (initvar = 0; initvar < 256; initvar = initvar+1)
    file[initvar] = _RAND_0[31:0];
  `endif // RANDOMIZE_MEM_INIT
  _RAND_1 = {1{`RANDOM}};
  `ifdef RANDOMIZE_MEM_INIT
  for (initvar = 0; initvar < 256; initvar = initvar+1)
    code[initvar] = _RAND_1[31:0];
  `endif // RANDOMIZE_MEM_INIT
  `ifdef RANDOMIZE_REG_INIT
  _RAND_2 = {1{`RANDOM}};
  pc = _RAND_2[7:0];
  `endif // RANDOMIZE_REG_INIT
  `endif // RANDOMIZE
end // initial
`endif // SYNTHESIS
  always @(posedge clock) begin
    if(file__T_12_en & file__T_12_mask) begin
      file[file__T_12_addr] <= file__T_12_data; // @[Risc.scala 14:17]
    end
    if(code__T_4_en & code__T_4_mask) begin
      code[code__T_4_addr] <= code__T_4_data; // @[Risc.scala 15:17]
    end
    if (reset) begin
      pc <= 8'h0;
    end else if (!(io_isWr)) begin
      if (io_boot) begin
        pc <= 8'h0;
      end else begin
        pc <= _T_14;
      end
    end
  end
endmodule
回复

使用道具 举报

您需要登录后才可以回帖 注册/登录

本版积分规则

关闭

站长推荐上一条 /2 下一条



手机版|小黑屋|与非网

GMT+8, 2024-3-29 22:03 , Processed in 0.113207 second(s), 15 queries , MemCache On.

ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

苏公网安备 32059002001037号

Powered by Discuz! X3.4

Copyright © 2001-2020, Tencent Cloud.