Article #963

既に発行済みのブログであっても適宜修正・追加することがあります。
We may make changes and additions to blogs already published.

Pongと強化学習 (90)

posted by sakurai on March 21, 2025 #963

TbのBSVソースを示します。

Tb.bsv:

    import StmtFSM::*;
    import DumpFSM::*;
    import Uart::*;
    import RegFile::*;
    import Connectable::*;
    
    typedef Bit#(16) Addr;
    typedef Bit#(4) Data;
    
    `define STX 8'h02
    
    (* synthesize *)
    module mkTb();
       RegFile#(Addr, Data) vram <- mkRegFileLoad("data.hex", 0, 256*256-1);
       Reg#(Bool) cwait <- mkReg(False);
       Reg#(Bool) game <- mkReg(True);
       Reg#(Bool) tic <- mkReg(False);
         
       DumpFSM_ifc dump <- mkDumpFSM();
       Uart_ifc uart <- mkUart();
       
       mkConnection(cwait, dump.cwaitm);
       mkConnection(dump.datam, vram.sub(dump.addrm));
       mkConnection(tic, dump.c60Hz);
       mkConnection(game, dump.mode);
       mkConnection(uart.sout, dump.sin);
       mkConnection(uart.sin, dump.sout);
    
       Stmt test =
       seq
          // Game Mode: ticをスルーする
          game <= True;
          cwait <= False;
          repeat(100) noAction;
          // 非Game Mode: GameFSMに停止要求を出して停止したらダンプする
          game <= False;
          uart.write(8'h31);
          uart.write(8'h32);
          uart.write(8'h33);
          uart.write(`STX); // ここからダンプスタート
          uart.write(8'h31);
          uart.write(8'h32);
          uart.write(8'h33);
          cwait <= True; // GameFSMが停止したシミュレーション
          repeat(20) uart.write(8'h31);
          repeat(100) noAction;
          game <= True;
          await(!dump.selm()); // DumpFSMが動作完了し、SELをOFF
          repeat(100) noAction;
          $finish;
       endseq;
    
       mkAutoFSM(test);
    
        // クロックのトグルをカウントするレジスタ
        Reg#(Bit#(4)) counter <- mkReg(0);
        // カウンターが特定の値に達したらクロックをトグル
        rule toggleClock;
            if (counter == 5) begin // ここで分周比を調整
                tic <= !tic;
                counter <= 0;
            end else begin
                counter <= counter + 1;
            end
        endrule
        
    endmodule

左矢前のブログ 次のブログ右矢

Leave a Comment

Your email address will not be published.

You may use Markdown syntax. If you include an ad such as http://, it will be invalidated by our AI system.

Please enter the numbers as they are shown in the image above.