aidanozo

Untitled

Dec 14th, 2025
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VeriLog 13.54 KB | None | 0 0
  1. `timescale 1ns / 1ps
  2.  
  3. module uc(
  4.         clk,
  5.         rst,
  6.         ri,
  7.         ind,
  8.         regs_addr,
  9.         regs_oe,
  10.         regs_we,
  11.         alu_oe,
  12.         alu_carry,
  13.         alu_opcode,
  14.         ram_oe,
  15.         ram_we,
  16.         io_oe,
  17.         io_we,
  18.         cp_oe,
  19.         cp_we,
  20.         ind_sel,
  21.         ind_oe,
  22.         ind_we,
  23.         am_oe,
  24.         am_we,
  25.         aie_oe,
  26.         aie_we,
  27.         t1_oe,
  28.         t1_we,
  29.         t2_oe,
  30.         t2_we,
  31.         ri_oe,
  32.         ri_we,
  33.         disp_state
  34.     );
  35.  
  36. parameter word_width  =         16;
  37. parameter state_width =         16;
  38.  
  39. `define ADC                     0
  40. `define SBB1                    1
  41. `define SBB2                    2
  42. `define NOT                     3
  43. `define AND                     4
  44. `define OR                      5
  45. `define XOR                     6
  46. `define SHL                     7
  47. `define SHR                     8
  48. `define SAR                     9
  49.  
  50. `define RA                      0
  51. `define RB                      1
  52. `define RC                      2
  53. `define IS                      3
  54. `define XA                      4
  55. `define XB                      5
  56. `define BA                      6
  57. `define BB                      7
  58.  
  59. input                           clk;
  60. input                           rst;
  61. input [word_width-1 : 0]        ri;
  62. input [word_width-1 : 0]        ind;
  63. output reg                      alu_oe;
  64. output reg                      alu_carry;
  65. output reg[3 : 0]               alu_opcode;
  66. output reg                      ram_oe;
  67. output reg                      ram_we;
  68. output reg                      io_oe;
  69. output reg                      io_we;
  70. output reg[2 : 0]               regs_addr;
  71. output reg                      regs_oe;
  72. output reg                      regs_we;
  73. output reg                      cp_oe;
  74. output reg                      cp_we;
  75. output reg                      ind_sel;        // controls IND register input (0 = bus, 1 = alu flags)
  76. output reg                      ind_oe;
  77. output reg                      ind_we;
  78. output reg                      am_oe;
  79. output reg                      am_we;
  80. output reg                      aie_oe;
  81. output reg                      aie_we;
  82. output reg                      t1_oe;
  83. output reg                      t1_we;
  84. output reg                      t2_oe;
  85. output reg                      t2_we;
  86. output reg                      ri_oe;          
  87. output reg                      ri_we;
  88. output[state_width-1 : 0]       disp_state;
  89.  
  90. wire [0:6]                      cop;
  91. wire                            d;
  92. wire [0:1]                      mod;
  93. wire [0:2]                      rg;
  94. wire [0:2]                      rm;
  95.  
  96. assign cop  = {ri[0], ri[1], ri[2], ri[3], ri[4], ri[5], ri[6]};
  97. assign d    = {ri[7]};
  98. assign mod  = {ri[8], ri[9]};
  99. assign rg   = {ri[10], ri[11], ri[12]};
  100. assign rm   = {ri[13], ri[14], ri[15]};
  101.  
  102. `define reset                   'h00            // reset state
  103. `define fetch                   'h10            // load instruction to instruction register
  104. `define decode                  'h20            // analyze loaded instruction
  105. `define addr_sum                'h30            // computes address of the form [By+Xz] with y,z in {A, B}
  106. `define addr_reg                'h34            // computes address of the form [yz] with y in {X, B} and z in {A, B}
  107. `define load_src_reg            'h40            // load source operand from register
  108. `define load_src_mem            'h44            // load source operand from memory
  109. `define load_dst_reg            'h50            // load destination operand from register
  110. `define load_dst_mem            'h54            // load destination operand from memory
  111. `define exec_1op                'h60            // execute 1 operand instructions
  112. `define exec_2op                'h64            // execute 2 operand instructions
  113. `define store_reg               'h70            // store result to register
  114. `define store_mem               'h74            // store result to memory
  115. `define inc_cp                  'h80            // increment program counter
  116.  
  117. reg [state_width-1 : 0] state = `reset, state_next;
  118. reg [state_width-1 : 0] decoded_src, decoded_src_next;      // stores decoded source operand load state
  119. reg [state_width-1 : 0] decoded_dst, decoded_dst_next;      // stores decoded destination operand load state
  120. reg [state_width-1 : 0] decoded_exec, decoded_exec_next;    // stores decoded execute state
  121. reg [state_width-1 : 0] decoded_store, decoded_store_next;  // stores decoded store state
  122. reg decoded_d, decoded_d_next;                              // stores decoded direction bit
  123.  
  124. // FSM - sequential part
  125. always @(posedge clk) begin
  126.     state <= `reset;
  127.  
  128.     if(!rst) begin
  129.         state <= state_next;
  130.  
  131.         if(state == `decode) begin
  132.             decoded_src     <= decoded_src_next;
  133.             decoded_dst     <= decoded_dst_next;
  134.             decoded_exec    <= decoded_exec_next;
  135.             decoded_store   <= decoded_store_next;
  136.             decoded_d       <= decoded_d_next;
  137.         end
  138.     end
  139. end
  140.  
  141. // FSM - combinational part
  142. always @(*) begin
  143.     state_next          = `reset;
  144.     decoded_src_next    = `reset;
  145.     decoded_dst_next    = `reset;
  146.     decoded_exec_next   = `reset;
  147.     decoded_store_next  = `reset;
  148.     decoded_d_next      = 0;
  149.     alu_oe              = 0;
  150.     alu_carry           = 0;
  151.     alu_opcode          = 0;
  152.     ram_oe              = 0;
  153.     ram_we              = 0;
  154.     io_oe               = 0;
  155.     io_we               = 0;
  156.     regs_addr           = 0;
  157.     regs_oe             = 0;
  158.     regs_we             = 0;
  159.     cp_oe               = 0;
  160.     cp_we               = 0;
  161.     ind_sel             = 0;
  162.     ind_oe              = 0;
  163.     ind_we              = 0;
  164.     am_oe               = 0;
  165.     am_we               = 0;
  166.     aie_oe              = 0;
  167.     aie_we              = 0;
  168.     t1_oe               = 0;
  169.     t1_we               = 0;
  170.     t2_oe               = 0;
  171.     t2_we               = 0;
  172.     ri_oe               = 0;
  173.     ri_we               = 0;
  174.  
  175.     case(state)
  176.         `reset: begin
  177.             state_next  = `fetch;
  178.         end
  179.  
  180.         `fetch: begin
  181.             cp_oe       = 1;
  182.             am_we       = 1;
  183.             state_next  = `fetch + 1;
  184.         end
  185.  
  186.         `fetch + 'd1: begin
  187.             am_oe       = 1;
  188.             state_next  = `fetch + 2;
  189.         end
  190.  
  191.         `fetch + 'd2: begin
  192.             ram_oe      = 1;
  193.             ri_we       = 1;
  194.             state_next  = `decode;
  195.         end
  196.  
  197.         `decode: begin
  198.             // decode location of operands and operation
  199.             if(cop[0:3] == 4'b0001) begin           // one operand instructions
  200.                 decoded_d_next      = 0;
  201.                 decoded_dst_next    = mod == 2'b11 ? `load_dst_reg : `load_dst_mem;
  202.                 decoded_src_next    = decoded_dst_next;
  203.                 decoded_exec_next   = `exec_1op;
  204.                 decoded_store_next  = mod == 2'b11 ? `store_reg : `store_mem;
  205.             end
  206.             else if(cop[0:2] == 3'b010) begin       // two operand instructions
  207.                 decoded_d_next      = d;
  208.                 decoded_dst_next    = (mod == 2'b11) || (d == 1) ? `load_dst_reg : `load_dst_mem;
  209.                 decoded_src_next    = (mod == 2'b11) || (d == 0) ? `load_src_reg : `load_src_mem;
  210.                 decoded_exec_next   = `exec_2op;
  211.                 decoded_store_next  = !cop[3] ? `inc_cp : ((mod == 2'b11) || (d == 1) ? `store_reg : `store_mem);
  212.             end
  213.            
  214.             // decode address calculation mode
  215.             case(mod)
  216.                 2'b00: begin
  217.                     state_next = rm[0] ? `addr_reg : `addr_sum;
  218.                 end
  219.                
  220.                 2'b11: begin
  221.                     state_next = decoded_src_next;
  222.                 end
  223.             endcase
  224.         end
  225.        
  226.         `addr_sum: begin
  227.             regs_addr   = rm[1] ? `BB : `BA;
  228.             regs_oe     = 1;
  229.             t1_we       = 1;
  230.             state_next  = `addr_sum + 1;
  231.         end
  232.  
  233.         `addr_sum + 'd1: begin
  234.             regs_addr   = rm[2] ? `XB : `XA;
  235.             regs_oe     = 1;
  236.             t2_we       = 1;
  237.             state_next  = `addr_sum + 2;
  238.         end
  239.  
  240.         `addr_sum + 'd2: begin
  241.             t1_oe       = 1;
  242.             t2_oe       = 1;
  243.             alu_carry   = 0;
  244.             alu_opcode  = `ADC;
  245.             alu_oe      = 1;
  246.             if(decoded_d)
  247.                 t2_we = 1;
  248.             else
  249.                 t1_we = 1;
  250.             state_next  = decoded_src;
  251.         end
  252.        
  253.         `addr_reg: begin
  254.             regs_addr   = rm;
  255.             regs_oe     = 1;
  256.             if(decoded_d)
  257.                 t2_we = 1;
  258.             else
  259.                 t1_we = 1;
  260.             state_next  = decoded_src;
  261.         end
  262.        
  263.         `load_src_reg: begin
  264.             regs_addr   = decoded_d ? rm : rg;
  265.             regs_oe     = 1;
  266.             t2_we       = 1;
  267.             state_next  = decoded_dst;
  268.         end
  269.        
  270.         `load_src_mem: begin
  271.             t1_oe       = 0;
  272.             t2_oe       = 1;
  273.             alu_opcode  = `OR;
  274.             alu_oe      = 1;
  275.             am_we       = 1;
  276.             state_next  = `load_src_mem + 1;
  277.         end
  278.  
  279.         `load_src_mem + 'd1: begin
  280.             am_oe       = 1;
  281.             state_next  = `load_src_mem + 2;
  282.         end
  283.  
  284.         `load_src_mem + 'd2: begin
  285.             ram_oe      = 1;
  286.             t2_we       = 1;
  287.             state_next  = decoded_dst;
  288.         end
  289.  
  290.         `load_dst_reg: begin
  291.             regs_addr   = decoded_d ? rg : rm;
  292.             regs_oe     = 1;
  293.             t1_we       = 1;
  294.             state_next  = decoded_exec;
  295.         end
  296.        
  297.         `load_dst_mem: begin
  298.             t1_oe       = 1;
  299.             t2_oe       = 0;
  300.             alu_opcode  = `OR;
  301.             alu_oe      = 1;
  302.             am_we       = 1;
  303.             state_next  = `load_dst_mem + 1;
  304.         end
  305.  
  306.         `load_dst_mem + 'd1: begin
  307.             am_oe       = 1;
  308.             state_next  = `load_dst_mem + 2;
  309.         end
  310.  
  311.         `load_dst_mem + 'd2: begin
  312.             ram_oe      = 1;
  313.             t1_we       = 1;
  314.             state_next  = decoded_exec;
  315.         end
  316.  
  317.         `exec_1op: begin
  318.             t1_oe       = 1;
  319.             case(cop[4:6])
  320.                 3'b000: begin                               // INC
  321.                     alu_carry   = 1;
  322.                     alu_opcode  = `ADC;
  323.                 end
  324.                 3'b001: begin                               // DEC
  325.                     alu_carry   = 1;
  326.                     alu_opcode  = `SBB1;
  327.                 end
  328.                 3'b010: begin                               // NEG
  329.                     alu_carry   = 0;
  330.                     alu_opcode  = `SBB2;
  331.                 end
  332.                 3'b011: begin                               // NOT
  333.                     alu_opcode  = `NOT;
  334.                 end
  335.                 3'b100: alu_opcode = `SHL;                  // SHL/SAL
  336.                 3'b101: alu_opcode = `SHR;                  // SHR
  337.                 3'b110: alu_opcode = `SAR;                  // SAR
  338.             endcase
  339.             alu_oe      = 1;
  340.             t1_we       = 1;
  341.             ind_sel     = 1;
  342.             ind_we      = 1;
  343.             state_next  = decoded_store;
  344.         end
  345.        
  346.         `exec_2op: begin
  347.             t1_oe   = 1;
  348.             t2_oe   = 1;
  349.             case(cop[4:6])
  350.                 3'b000: begin                               // ADD
  351.                     alu_carry   = 0;
  352.                     alu_opcode  = `ADC;
  353.                 end
  354.                 3'b001: begin                               // ADC
  355.                     alu_carry   = ind[0];
  356.                     alu_opcode  = `ADC;
  357.                 end
  358.                 3'b010: begin                               // SUB/CMP
  359.                     alu_carry   = 0;
  360.                     alu_opcode  = `SBB1;
  361.                 end
  362.                 3'b011: begin                               // SBB
  363.                     alu_carry   = ind[0];
  364.                     alu_opcode  = `SBB1;
  365.                 end
  366.                 3'b100: alu_opcode = `AND;                  // AND/TEST
  367.                 3'b101: alu_opcode = `OR;                   // OR
  368.                 3'b110: alu_opcode = `XOR;                  // XOR
  369.             endcase
  370.             alu_oe      = 1;
  371.             t1_we       = 1;
  372.             ind_sel     = 1;
  373.             ind_we      = 1;
  374.             state_next  = decoded_store;
  375.         end
  376.        
  377.         `store_reg: begin
  378.             t1_oe       = 1;
  379.             t2_oe       = 0;
  380.             alu_opcode  = `OR;
  381.             alu_oe      = 1;
  382.             regs_addr   = decoded_d ? rg : rm;
  383.             regs_we     = 1;
  384.             state_next  = `inc_cp;
  385.         end
  386.        
  387.         `store_mem: begin
  388.             t1_oe       = 1;
  389.             t2_oe       = 0;
  390.             alu_opcode  = `OR;
  391.             alu_oe      = 1;
  392.             am_oe       = 1;
  393.             ram_we      = 1;
  394.             state_next  = `store_mem + 1;
  395.         end
  396.  
  397.         `store_mem + 'd1: begin
  398.             state_next  = `inc_cp;
  399.         end
  400.  
  401.         `inc_cp: begin
  402.             cp_oe       = 1;
  403.             t1_we       = 1;
  404.             state_next  = `inc_cp + 1;
  405.         end
  406.  
  407.         `inc_cp + 'd1: begin
  408.             t1_oe       = 1;
  409.             cp_we       = 1;
  410.             alu_oe      = 1;
  411.             alu_carry   = 1;
  412.             alu_opcode  = `ADC;
  413.             state_next  = `fetch;
  414.         end
  415.  
  416.         default: ;
  417.  
  418.     endcase
  419. end
  420.  
  421. assign disp_state = state;
  422.  
  423. endmodule
Advertisement
Add Comment
Please, Sign In to add comment