0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

verilogHDL学習目的で簡易CPUを作った

0
Posted at

目的

趣味でFPGAを触り始めたのでverilogHDLの学習

使用物品

FPGA(TangNano9K)

仕様

概要

本回路はFPGA上に実装した簡易CPUである。
ROMに格納された命令を順番に実行し、加算・減算の結果をLEDへ出力する。

動作クロック

入力クロック:clk
div_cnt[24]を利用してCPU動作速度を低速化
CPUは分周後クロック cpu_clk で動作

レジスタ構成

R0 6bit 演算結果格納
PC 4bit 次に実行する命令アドレス
div_cnt 25bit クロック分周

命令

8bit構成で
[7:4] オペコード
[3:0] 即値

オペコード

0x1 ADD
0x2 SUB

コード

cpu.v
module cpu(
  input wire clk,
  input wire rst_n,
  output wire [5:0] led
);

reg [5:0] R0; //計算結果
reg [3:0] PC; //プログラムカウンタ次に読む命令番号

reg[7:0] ROM [0:15]; 

reg [24:0] div_cnt;

initial begin
    ROM[0] = 8'h15; // ADD 5
    ROM[1] = 8'h12; // ADD 2
    ROM[2] = 8'h21;; // SUB 1
    ROM[3] = 8'h23;; // SUB 3
end

wire [7:0] instr;
assign instr = ROM[PC];

wire cpu_clk;
assign cpu_clk = div_cnt[24];

always @(posedge clk or negedge rst_n)begin
  if(!rst_n)
    div_cnt <= 0;
  else 
    div_cnt <= div_cnt + 1;    
end

always @(posedge cpu_clk or negedge rst_n)begin
  if (!rst_n)begin
    R0 <= 0;
    PC <= 0;
  end
  
  else begin
    case(instr[7:4])
      4'h1:
        R0 <= R0 + instr[3:0];
      4'h2:
        R0 <= R0 - instr[3:0];
      default:
        ;
    endcase
    
    if(PC == 3)
      PC <= 0;
    else
      PC <= PC + 1;

  end
end

assign led = ~R0;

endmodule


動作確認(永遠に1加算)

感想

身についた部分はあるかなと思います。ここから命令を増やしていきたいです。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?