aidanozo

Untitled

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