//---------------------------------------------------------------------------- // Copyright (C) 2001 Authors // // This source file may be used and distributed without restriction provided // that this copyright statement is not removed from the file and that any // derivative work contains the original copyright notice and the associated // disclaimer. // // This source file is free software; you can redistribute it and/or modify // it under the terms of the GNU Lesser General Public License as published // by the Free Software Foundation; either version 2.1 of the License, or // (at your option) any later version. // // This source is distributed in the hope that it will be useful, but WITHOUT // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or // FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public // License for more details. // // You should have received a copy of the GNU Lesser General Public License // along with this source; if not, write to the Free Software Foundation, // Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA // //---------------------------------------------------------------------------- // // *File Name: omsp_frontend.v // // *Module Description: // openMSP430 Instruction fetch and decode unit // // *Author(s): // - Olivier Girard, olgirard@gmail.com // //---------------------------------------------------------------------------- // $Rev: 60 $ // $LastChangedBy: olivier.girard $ // $LastChangedDate: 2010-02-03 22:12:25 +0100 (Wed, 03 Feb 2010) $ //---------------------------------------------------------------------------- `include "timescale.v" `include "openMSP430_defines.v" module omsp_frontend ( // OUTPUTs dbg_halt_st, // Halt/Run status from CPU decode_noirq, // Frontend decode instruction e_state, // Execution state exec_done, // Execution completed inst_ad, // Decoded Inst: destination addressing mode inst_as, // Decoded Inst: source addressing mode inst_alu, // ALU control signals inst_bw, // Decoded Inst: byte width inst_dest, // Decoded Inst: destination (one hot) inst_dext, // Decoded Inst: destination extended instruction word inst_irq_rst, // Decoded Inst: Reset interrupt inst_jmp, // Decoded Inst: Conditional jump inst_sext, // Decoded Inst: source extended instruction word inst_so, // Decoded Inst: Single-operand arithmetic inst_src, // Decoded Inst: source (one hot) inst_type, // Decoded Instruction type irq_acc, // Interrupt request accepted (one-hot signal) mab, // Frontend Memory address bus mb_en, // Frontend Memory bus enable nmi_acc, // Non-Maskable interrupt request accepted pc, // Program counter pc_nxt, // Next PC value (for CALL & IRQ) // INPUTs cpuoff, // Turns off the CPU dbg_halt_cmd, // Halt CPU command dbg_reg_sel, // Debug selected register for rd/wr access fe_pmem_wait, // Frontend wait for Instruction fetch gie, // General interrupt enable irq, // Maskable interrupts mclk, // Main system clock mdb_in, // Frontend Memory data bus input nmi_evt, // Non-maskable interrupt event pc_sw, // Program counter software value pc_sw_wr, // Program counter software write puc, // Main system reset wdt_irq // Watchdog-timer interrupt ); // OUTPUTs //========= output dbg_halt_st; // Halt/Run status from CPU output decode_noirq; // Frontend decode instruction output [3:0] e_state; // Execution state output exec_done; // Execution completed output [7:0] inst_ad; // Decoded Inst: destination addressing mode output [7:0] inst_as; // Decoded Inst: source addressing mode output [11:0] inst_alu; // ALU control signals output inst_bw; // Decoded Inst: byte width output [15:0] inst_dest; // Decoded Inst: destination (one hot) output [15:0] inst_dext; // Decoded Inst: destination extended instruction word output inst_irq_rst; // Decoded Inst: Reset interrupt output [7:0] inst_jmp; // Decoded Inst: Conditional jump output [15:0] inst_sext; // Decoded Inst: source extended instruction word output [7:0] inst_so; // Decoded Inst: Single-operand arithmetic output [15:0] inst_src; // Decoded Inst: source (one hot) output [2:0] inst_type; // Decoded Instruction type output [13:0] irq_acc; // Interrupt request accepted (one-hot signal) output [15:0] mab; // Frontend Memory address bus output mb_en; // Frontend Memory bus enable output nmi_acc; // Non-Maskable interrupt request accepted output [15:0] pc; // Program counter output [15:0] pc_nxt; // Next PC value (for CALL & IRQ) // INPUTs //========= input cpuoff; // Turns off the CPU input dbg_halt_cmd; // Halt CPU command input [3:0] dbg_reg_sel; // Debug selected register for rd/wr access input fe_pmem_wait; // Frontend wait for Instruction fetch input gie; // General interrupt enable input [13:0] irq; // Maskable interrupts input mclk; // Main system clock input [15:0] mdb_in; // Frontend Memory data bus input input nmi_evt; // Non-maskable interrupt event input [15:0] pc_sw; // Program counter software value input pc_sw_wr; // Program counter software write input puc; // Main system reset input wdt_irq; // Watchdog-timer interrupt //============================================================================= // 1) FRONTEND STATE MACHINE //============================================================================= // The wire "conv" is used as state bits to calculate the next response reg [2:0] i_state; reg [2:0] i_state_nxt; reg [1:0] inst_sz; wire [1:0] inst_sz_nxt; wire irq_detect; wire [2:0] inst_type_nxt; wire is_const; reg [15:0] sconst_nxt; reg [3:0] e_state_nxt; // State machine definitons parameter I_IRQ_FETCH = 3'h0; parameter I_IRQ_DONE = 3'h1; parameter I_DEC = 3'h2; // New instruction ready for decode parameter I_EXT1 = 3'h3; // 1st Extension word parameter I_EXT2 = 3'h4; // 2nd Extension word parameter I_IDLE = 3'h5; // CPU is in IDLE mode // States Transitions always @(i_state or inst_sz or inst_sz_nxt or pc_sw_wr or exec_done or exec_done or irq_detect or cpuoff or dbg_halt_cmd or e_state) case(i_state) I_IDLE : i_state_nxt = (irq_detect & ~dbg_halt_cmd) ? I_IRQ_FETCH : (~cpuoff & ~dbg_halt_cmd) ? I_DEC : I_IDLE; I_IRQ_FETCH: i_state_nxt = I_IRQ_DONE; I_IRQ_DONE : i_state_nxt = I_DEC; I_DEC : i_state_nxt = irq_detect ? I_IRQ_FETCH : (cpuoff | dbg_halt_cmd) & exec_done ? I_IDLE : dbg_halt_cmd & (e_state==`E_IDLE) ? I_IDLE : pc_sw_wr ? I_DEC : ~exec_done & ~(e_state==`E_IDLE) ? I_DEC : // Wait in decode state (inst_sz_nxt!=2'b00) ? I_EXT1 : I_DEC; // until execution is completed I_EXT1 : i_state_nxt = irq_detect ? I_IRQ_FETCH : pc_sw_wr ? I_DEC : (inst_sz!=2'b01) ? I_EXT2 : I_DEC; I_EXT2 : i_state_nxt = irq_detect ? I_IRQ_FETCH : I_DEC; default : i_state_nxt = I_IRQ_FETCH; endcase // State machine always @(posedge mclk or posedge puc) if (puc) i_state <= I_IRQ_FETCH; else i_state <= i_state_nxt; // Utility signals wire decode_noirq = ((i_state==I_DEC) & (exec_done | (e_state==`E_IDLE))); wire decode = decode_noirq | irq_detect; wire fetch = ~((i_state==I_DEC) & ~(exec_done | (e_state==`E_IDLE))) & ~(e_state_nxt==`E_IDLE); // Debug interface cpu status reg dbg_halt_st; always @(posedge mclk or posedge puc) if (puc) dbg_halt_st <= 1'b0; else dbg_halt_st <= dbg_halt_cmd & (i_state_nxt==I_IDLE); //============================================================================= // 2) INTERRUPT HANDLING //============================================================================= // Detect nmi interrupt reg inst_nmi; always @(posedge mclk or posedge puc) if (puc) inst_nmi <= 1'b0; else if (nmi_evt) inst_nmi <= 1'b1; else if (i_state==I_IRQ_DONE) inst_nmi <= 1'b0; // Detect reset interrupt reg inst_irq_rst; always @(posedge mclk or posedge puc) if (puc) inst_irq_rst <= 1'b1; else if (exec_done) inst_irq_rst <= 1'b0; // Detect other interrupts assign irq_detect = (inst_nmi | ((|irq | wdt_irq) & gie)) & ~dbg_halt_cmd & (exec_done | (i_state==I_IDLE)); // Select interrupt vector reg [3:0] irq_num; always @(posedge mclk or posedge puc) if (puc) irq_num <= 4'hf; else if (irq_detect) irq_num <= inst_nmi ? 4'he : irq[13] ? 4'hd : irq[12] ? 4'hc : irq[11] ? 4'hb : (irq[10] | wdt_irq) ? 4'ha : irq[9] ? 4'h9 : irq[8] ? 4'h8 : irq[7] ? 4'h7 : irq[6] ? 4'h6 : irq[5] ? 4'h5 : irq[4] ? 4'h4 : irq[3] ? 4'h3 : irq[2] ? 4'h2 : irq[1] ? 4'h1 : irq[0] ? 4'h0 : 4'hf; wire [15:0] irq_addr = {11'h7ff, irq_num, 1'b0}; // Interrupt request accepted wire [15:0] irq_acc_all = (16'h0001 << irq_num) & {16{(i_state==I_IRQ_FETCH)}}; wire [13:0] irq_acc = irq_acc_all[13:0]; wire nmi_acc = irq_acc_all[14]; //============================================================================= // 3) FETCH INSTRUCTION //============================================================================= // // 3.1) PROGRAM COUNTER & MEMORY INTERFACE //----------------------------------------- // Program counter reg [15:0] pc; // Compute next PC value wire [15:0] pc_incr = pc + {14'h0000, fetch, 1'b0}; wire [15:0] pc_nxt = pc_sw_wr ? pc_sw : (i_state==I_IRQ_FETCH) ? irq_addr : (i_state==I_IRQ_DONE) ? mdb_in : pc_incr; always @(posedge mclk or posedge puc) if (puc) pc <= 16'h0000; else pc <= pc_nxt; // Check if ROM has been busy in order to retry ROM access reg pmem_busy; always @(posedge mclk or posedge puc) if (puc) pmem_busy <= 16'h0000; else pmem_busy <= fe_pmem_wait; // Memory interface wire [15:0] mab = pc_nxt; wire mb_en = fetch | pc_sw_wr | (i_state==I_IRQ_FETCH) | pmem_busy | (dbg_halt_st & ~dbg_halt_cmd); // // 3.2) INSTRUCTION REGISTER //-------------------------------- // Instruction register wire [15:0] ir = mdb_in; // Detect if source extension word is required wire is_sext = (inst_as[`IDX] | inst_as[`SYMB] | inst_as[`ABS] | inst_as[`IMM]); // Detect if destination extension word is required wire is_dext = (inst_ad[`IDX] | inst_ad[`SYMB] | inst_ad[`ABS]); // For the Symbolic addressing mode, add -2 to the extension word in order // to make up for the PC address wire [15:0] ext_incr = ((i_state==I_EXT1) & inst_as[`SYMB]) | ((i_state==I_EXT2) & inst_ad[`SYMB]) | ((i_state==I_EXT1) & ~inst_as[`SYMB] & ~(i_state_nxt==I_EXT2) & inst_ad[`SYMB]) ? 16'hfffe : 16'h0000; wire [15:0] ext_nxt = ir + ext_incr; // Store source extension word reg [15:0] inst_sext; always @(posedge mclk or posedge puc) if (puc) inst_sext <= 16'h0000; else if (decode & is_const) inst_sext <= sconst_nxt; else if (decode & inst_type_nxt[`INST_JMP]) inst_sext <= {{5{ir[9]}},ir[9:0],1'b0}; else if ((i_state==I_EXT1) & is_sext) inst_sext <= ext_nxt; // Source extension word is ready wire inst_sext_rdy = (i_state==I_EXT1) & is_sext; // Store destination extension word reg [15:0] inst_dext; always @(posedge mclk or posedge puc) if (puc) inst_dext <= 16'h0000; else if ((i_state==I_EXT1) & ~is_sext) inst_dext <= ext_nxt; else if (i_state==I_EXT2) inst_dext <= ext_nxt; // Destination extension word is ready wire inst_dext_rdy = (((i_state==I_EXT1) & ~is_sext) | (i_state==I_EXT2)); //============================================================================= // 4) DECODE INSTRUCTION //============================================================================= // // 4.1) OPCODE: INSTRUCTION TYPE //---------------------------------------- // Instructions type is encoded in a one hot fashion as following: // // 3'b001: Single-operand arithmetic // 3'b010: Conditional jump // 3'b100: Two-operand arithmetic reg [2:0] inst_type; assign inst_type_nxt = {(ir[15:14]!=2'b00), (ir[15:13]==3'b001), (ir[15:13]==3'b000)} & {3{~irq_detect}}; always @(posedge mclk or posedge puc) if (puc) inst_type <= 3'b000; else if (decode) inst_type <= inst_type_nxt; // // 4.2) OPCODE: SINGLE-OPERAND ARITHMETIC //---------------------------------------- // Instructions are encoded in a one hot fashion as following: // // 8'b00000001: RRC // 8'b00000010: SWPB // 8'b00000100: RRA // 8'b00001000: SXT // 8'b00010000: PUSH // 8'b00100000: CALL // 8'b01000000: RETI // 8'b10000000: IRQ reg [7:0] inst_so; wire [7:0] inst_so_nxt = irq_detect ? 8'h80 : ((8'h01<