|
| 1 | +--============================================================================== |
| 2 | +-- File: ctrl_fsm.vhd |
| 3 | +-- Author: Pietro Lorefice |
| 4 | +--============================================================================== |
| 5 | +-- Description: |
| 6 | +-- FSM portion of the FSMD processor architecture. It keeps track of the |
| 7 | +-- internal state and provides the datapath with the correct signals. |
| 8 | +-- |
| 9 | +--============================================================================== |
| 10 | + |
| 11 | +library ieee; |
| 12 | +use ieee.std_logic_1164.all; |
| 13 | + |
| 14 | +entity ctrl_fsm is |
| 15 | + port ( |
| 16 | + clk : in std_logic; -- Clock |
| 17 | + rst : in std_logic; -- Reset |
| 18 | + opcode : in std_logic_vector(3 downto 0); -- Instruction opcode |
| 19 | + |
| 20 | + alu_op_b_sel : out std_logic; -- ALU operand B select |
| 21 | + alu_ctrl_op : out std_logic_vector(1 downto 0); -- ALU control unit operation |
| 22 | + |
| 23 | + pc_en : out std_logic; -- Program counter register enable |
| 24 | + ir_en : out std_logic; -- Instruction register enable |
| 25 | + |
| 26 | + reg_we_l : out std_logic; -- Register file write enable |
| 27 | + reg_op_a_sel : out std_logic; -- Register file operand A select |
| 28 | + reg_op_b_sel : out std_logic; -- Register file operand B select |
| 29 | + reg_wr_d_sel : out std_logic; -- Register file write data select |
| 30 | + |
| 31 | + mem_sel_l : out std_logic; -- Data memory select |
| 32 | + mem_we_l : out std_logic -- Data memory write enable |
| 33 | + ); |
| 34 | +end entity ctrl_fsm; |
| 35 | + |
| 36 | +architecture RTL of ctrl_fsm is |
| 37 | + -- ================== |
| 38 | + -- | State register | |
| 39 | + -- ================== |
| 40 | + type state_t is (fetch, fetch_w, |
| 41 | + decode, |
| 42 | + read, |
| 43 | + execute, |
| 44 | + write |
| 45 | + ); |
| 46 | + |
| 47 | + signal state_q, state_n : state_t; |
| 48 | + |
| 49 | + -- ==================== |
| 50 | + -- | Output registers | |
| 51 | + -- ==================== |
| 52 | + signal alu_op_b_sel_q, alu_op_b_sel_n : std_logic; |
| 53 | + signal alu_ctrl_op_q, alu_ctrl_op_n : std_logic_vector(1 downto 0); |
| 54 | + |
| 55 | + signal pc_en_q, pc_en_n : std_logic; |
| 56 | + signal ir_en_q, ir_en_n : std_logic; |
| 57 | + |
| 58 | + signal reg_we_l_q, reg_we_l_n : std_logic; |
| 59 | + signal reg_op_a_sel_q, reg_op_a_sel_n : std_logic; |
| 60 | + signal reg_op_b_sel_q, reg_op_b_sel_n : std_logic; |
| 61 | + signal reg_wr_d_sel_q, reg_wr_d_sel_n : std_logic; |
| 62 | + |
| 63 | + signal mem_sel_l_q, mem_sel_l_n : std_logic; |
| 64 | + signal mem_we_l_q, mem_we_l_n : std_logic; |
| 65 | + |
| 66 | +begin |
| 67 | + |
| 68 | + -- ================== |
| 69 | + -- | State register | |
| 70 | + -- ================== |
| 71 | + star : process(clk) is |
| 72 | + begin |
| 73 | + if rising_edge(clk) then |
| 74 | + if rst = '1' then |
| 75 | + state_q <= fetch; |
| 76 | + |
| 77 | + alu_ctrl_op_q <= "00"; |
| 78 | + alu_op_b_sel_q <= '0'; |
| 79 | + |
| 80 | + pc_en_q <= '0'; |
| 81 | + ir_en_q <= '0'; |
| 82 | + |
| 83 | + reg_we_l_q <= '1'; |
| 84 | + reg_op_a_sel_q <= '0'; |
| 85 | + reg_op_b_sel_q <= '0'; |
| 86 | + reg_wr_d_sel_q <= '0'; |
| 87 | + |
| 88 | + mem_sel_l_q <= '1'; |
| 89 | + mem_we_l_q <= '1'; |
| 90 | + else |
| 91 | + state_q <= state_n; |
| 92 | + |
| 93 | + alu_ctrl_op_q <= alu_ctrl_op_n; |
| 94 | + alu_op_b_sel_q <= alu_op_b_sel_n; |
| 95 | + |
| 96 | + pc_en_q <= pc_en_n; |
| 97 | + ir_en_q <= ir_en_n; |
| 98 | + |
| 99 | + reg_we_l_q <= reg_we_l_n; |
| 100 | + reg_op_a_sel_q <= reg_op_a_sel_n; |
| 101 | + reg_op_b_sel_q <= reg_op_b_sel_n; |
| 102 | + reg_wr_d_sel_q <= reg_wr_d_sel_n; |
| 103 | + |
| 104 | + mem_sel_l_q <= mem_sel_l_n; |
| 105 | + mem_we_l_q <= mem_we_l_n; |
| 106 | + end if; |
| 107 | + end if; |
| 108 | + end process star; |
| 109 | + |
| 110 | + -- ============= |
| 111 | + -- | FSM logic | |
| 112 | + -- ============= |
| 113 | + fsm : process(state_q, opcode, |
| 114 | + alu_ctrl_op_q, alu_op_b_sel_q, |
| 115 | + ir_en_q, pc_en_q, |
| 116 | + mem_sel_l_q, mem_we_l_q, |
| 117 | + reg_op_a_sel_q, reg_op_b_sel_q, reg_we_l_q, reg_wr_d_sel_q |
| 118 | + ) is |
| 119 | + begin |
| 120 | + state_n <= state_q; |
| 121 | + |
| 122 | + alu_ctrl_op_n <= alu_ctrl_op_q; |
| 123 | + alu_op_b_sel_n <= alu_op_b_sel_q; |
| 124 | + |
| 125 | + pc_en_n <= pc_en_q; |
| 126 | + ir_en_n <= ir_en_q; |
| 127 | + |
| 128 | + reg_we_l_n <= reg_we_l_q; |
| 129 | + reg_op_a_sel_n <= reg_op_a_sel_q; |
| 130 | + reg_op_b_sel_n <= reg_op_b_sel_q; |
| 131 | + reg_wr_d_sel_n <= reg_wr_d_sel_q; |
| 132 | + |
| 133 | + mem_sel_l_n <= mem_sel_l_q; |
| 134 | + mem_we_l_n <= mem_we_l_q; |
| 135 | + |
| 136 | + case state_q is |
| 137 | + when fetch => |
| 138 | + state_n <= fetch_w; |
| 139 | + |
| 140 | + reg_we_l_n <= '1'; |
| 141 | + mem_sel_l_n <= '1'; |
| 142 | + mem_we_l_n <= '1'; |
| 143 | + |
| 144 | + pc_en_n <= '1'; |
| 145 | + |
| 146 | + when fetch_w => |
| 147 | + state_n <= decode; |
| 148 | + |
| 149 | + pc_en_n <= '0'; |
| 150 | + ir_en_n <= '1'; |
| 151 | + |
| 152 | + when decode => |
| 153 | + state_n <= read; |
| 154 | + |
| 155 | + ir_en_n <= '0'; |
| 156 | + |
| 157 | + case opcode is |
| 158 | + when X"0" | X"1" => |
| 159 | + reg_op_a_sel_n <= '1'; -- Ra |
| 160 | + reg_op_b_sel_n <= '1'; -- Rb |
| 161 | + reg_wr_d_sel_n <= '1'; -- ALU result |
| 162 | + state_n <= execute; -- No read |
| 163 | + |
| 164 | + when others => |
| 165 | + null; |
| 166 | + end case; |
| 167 | + |
| 168 | + when read => |
| 169 | + state_n <= execute; |
| 170 | + |
| 171 | + when execute => |
| 172 | + state_n <= write; |
| 173 | + |
| 174 | + case opcode is |
| 175 | + when X"0" | X"1" => |
| 176 | + alu_ctrl_op_n <= "10"; -- Ra + Rb |
| 177 | + alu_op_b_sel_n <= '0'; -- Rb |
| 178 | + |
| 179 | + when others => |
| 180 | + null; |
| 181 | + end case; |
| 182 | + |
| 183 | + when write => |
| 184 | + state_n <= fetch; |
| 185 | + |
| 186 | + case opcode is |
| 187 | + when X"0" | X"1" => |
| 188 | + reg_we_l_n <= '0'; |
| 189 | + |
| 190 | + when others => |
| 191 | + null; |
| 192 | + end case; |
| 193 | + end case; |
| 194 | + end process fsm; |
| 195 | + |
| 196 | + -- ====================== |
| 197 | + -- | Output assignments | |
| 198 | + -- ====================== |
| 199 | + alu_op_b_sel <= alu_op_b_sel_q; |
| 200 | + alu_ctrl_op <= alu_ctrl_op_q; |
| 201 | + pc_en <= pc_en_q; |
| 202 | + ir_en <= ir_en_q; |
| 203 | + reg_we_l <= reg_we_l_q; |
| 204 | + reg_op_a_sel <= reg_op_a_sel_q; |
| 205 | + reg_op_b_sel <= reg_op_b_sel_q; |
| 206 | + reg_wr_d_sel <= reg_wr_d_sel_q; |
| 207 | + mem_sel_l <= mem_sel_l_q; |
| 208 | + mem_we_l <= mem_we_l_q; |
| 209 | + |
| 210 | + |
| 211 | +end architecture RTL; |
0 commit comments