1 `include "ARM_Constants.v"
 
  10         output reg [31:0] op0,
 
  11         output reg [31:0] op1,
 
  12         output reg [31:0] op2,
 
  14         output reg [31:0] outcpsr,
 
  15         output reg [31:0] outspsr,
 
  17         output reg [3:0] read_0,
 
  18         output reg [3:0] read_1,
 
  19         output reg [3:0] read_2,
 
  25         wire [31:0] regs0, regs1, regs2;
 
  27         reg [31:0] op0_out, op1_out, op2_out;
 
  31         wire [31:0] shift_oper;
 
  32         wire [31:0] shift_res;
 
  34         wire [31:0] rotate_res;
 
  36         assign regs0 = (read_0 == 4'b1111) ? rpc : rdata_0;
 
  37         assign regs1 = (read_1 == 4'b1111) ? rpc : rdata_1;
 
  38         assign regs2 = rdata_2; /* use regs2 for things that cannot be r15 */
 
  40         IREALLYHATEARMSHIFT shift(.insn(insn),
 
  43                                   .cflag_in(incpsr[`CPSR_C]),
 
  45                                   .cflag_out(shift_cflag_out));
 
  47         SuckLessRotator whirr(.oper({24'b0, insn[7:0]}),
 
  53                 `DECODE_ALU_MULT,               /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
 
  54 //              `DECODE_ALU_MUL_LONG,           /* Multiply long */
 
  55                 `DECODE_ALU_MRS,                /* MRS (Transfer PSR to register) */
 
  56                 `DECODE_ALU_MSR,                /* MSR (Transfer register to PSR) */
 
  57                 `DECODE_ALU_MSR_FLAGS,          /* MSR (Transfer register or immediate to PSR, flag bits only) */
 
  58                 `DECODE_ALU_SWP,                /* Atomic swap */
 
  59                 `DECODE_ALU_BX,                 /* Branch and exchange */
 
  60                 `DECODE_ALU_HDATA_REG,          /* Halfword transfer - register offset */
 
  61                 `DECODE_ALU_HDATA_IMM,          /* Halfword transfer - register offset */
 
  62                 `DECODE_LDRSTR_UNDEFINED,       /* Undefined. I hate ARM */
 
  63                 `DECODE_LDRSTR,                 /* Single data transfer */
 
  64                 `DECODE_LDMSTM,                 /* Block data transfer */
 
  65                 `DECODE_BRANCH,                 /* Branch */
 
  66                 `DECODE_LDCSTC,                 /* Coprocessor data transfer */
 
  67                 `DECODE_CDP,                    /* Coprocessor data op */
 
  68                 `DECODE_SWI:                    /* SWI */
 
  70                 `DECODE_MRCMCR:                 /* Coprocessor register transfer */
 
  72                 `DECODE_ALU:                    /* ALU */
 
  73                         rpc = inpc + (insn[25] ? 8 : (insn[4] ? 12 : 8));
 
  74                 default:                        /* X everything else out */
 
  83                 op0_out = 32'hxxxxxxxx;
 
  84                 op1_out = 32'hxxxxxxxx;
 
  85                 op2_out = 32'hxxxxxxxx;
 
  89                 `DECODE_ALU_MULT:       /* Multiply -- must come before ALU, because it pattern matches a specific case of ALU */
 
  91                         read_0 = insn[15:12]; /* Rn */
 
  92                         read_1 = insn[3:0];   /* Rm */
 
  93                         read_2 = insn[11:8];  /* Rs */
 
  99 //              `DECODE_ALU_MUL_LONG:   /* Multiply long */
 
 101 //                      read_0 = insn[11:8]; /* Rn */
 
 102 //                      read_1 = insn[3:0];   /* Rm */
 
 103 //                      read_2 = 4'b0;       /* anyus */
 
 107                 `DECODE_ALU_MRS:        /* MRS (Transfer PSR to register) */
 
 109                 `DECODE_ALU_MSR:        /* MSR (Transfer register to PSR) */
 
 111                         read_0 = insn[3:0];     /* Rm */
 
 115                 `DECODE_ALU_MSR_FLAGS:  /* MSR (Transfer register or immediate to PSR, flag bits only) */
 
 117                         read_0 = insn[3:0];     /* Rm */
 
 119                         if(insn[25]) begin     /* the constant case */
 
 120                                 op0_out = rotate_res;
 
 125                 `DECODE_ALU_SWP:        /* Atomic swap */
 
 127                         read_0 = insn[19:16]; /* Rn */
 
 128                         read_1 = insn[3:0];   /* Rm */
 
 133                 `DECODE_ALU_BX:         /* Branch and exchange */
 
 135                         read_0 = insn[3:0];   /* Rn */
 
 139                 `DECODE_ALU_HDATA_REG:  /* Halfword transfer - register offset */
 
 141                         read_0 = insn[19:16];
 
 143                         read_2 = insn[15:12];
 
 149                 `DECODE_ALU_HDATA_IMM:  /* Halfword transfer - immediate offset */
 
 151                         read_0 = insn[19:16];
 
 152                         read_1 = insn[15:12];
 
 155                         op1_out = {24'b0, insn[11:8], insn[3:0]};
 
 158                 `DECODE_ALU:            /* ALU */
 
 160                         read_0 = insn[19:16]; /* Rn */
 
 161                         read_1 = insn[3:0];   /* Rm */
 
 162                         read_2 = insn[11:8];  /* Rs for shift */
 
 165                         if(insn[25]) begin     /* the constant case */
 
 166                                 carry_out = incpsr[`CPSR_C];
 
 167                                 op1_out = rotate_res;
 
 169                                 carry_out = shift_cflag_out;
 
 173                 `DECODE_LDRSTR_UNDEFINED:       /* Undefined. I hate ARM */
 
 177                 `DECODE_LDRSTR:         /* Single data transfer */
 
 179                         read_0 = insn[19:16]; /* Rn */
 
 180                         read_1 = insn[3:0];   /* Rm */
 
 181                         read_2 = insn[15:12];
 
 185                                 op1_out = {20'b0, insn[11:0]};
 
 186                                 carry_out = incpsr[`CPSR_C];
 
 189                                 carry_out = shift_cflag_out;
 
 193                 `DECODE_LDMSTM:         /* Block data transfer */
 
 195                         read_0 = insn[19:16];
 
 198                         op1_out = {16'b0, insn[15:0]};
 
 200                 `DECODE_BRANCH:         /* Branch */
 
 202                         op0_out = {{6{insn[23]}}, insn[23:0], 2'b0};
 
 204                 `DECODE_LDCSTC:         /* Coprocessor data transfer */
 
 206                         read_0 = insn[19:16];
 
 209                         op1_out = {24'b0, insn[7:0]};
 
 211                 `DECODE_CDP:            /* Coprocessor data op */
 
 214                 `DECODE_MRCMCR:         /* Coprocessor register transfer */
 
 216                         read_0 = insn[15:12];
 
 220                 `DECODE_SWI:            /* SWI */
 
 224                         $display("Undecoded instruction");
 
 229         always @ (posedge clk) begin
 
 232                         op0 <= op0_out;   /* Rn - always */
 
 233                         op1 <= op1_out; /* 'operand 2' - Rm */
 
 234                         op2 <= op2_out;   /* thirdedge - Rs */
 
 243 module IREALLYHATEARMSHIFT(
 
 245         input [31:0] operand,
 
 246         input [31:0] reg_amt,
 
 248         output reg [31:0] res,
 
 251         wire [5:0] shift_amt;
 
 252         reg is_arith, is_rot;
 
 254         wire [31:0] rshift_res;
 
 256         assign shift_amt = insn[4] ? {|reg_amt[7:5], reg_amt[4:0]}     /* reg-specified shift */
 
 257                                    : {insn[11:7] == 5'b0, insn[11:7]}; /* immediate shift */
 
 259         SuckLessShifter barrel(.oper(operand),
 
 265                                .carryout(rshift_cout));
 
 289                 case (insn[6:5]) /* shift type */
 
 291                         {cflag_out, res} = {cflag_in, operand} << {insn[4] & shift_amt[5], shift_amt[4:0]};
 
 294                         cflag_out = rshift_cout;
 
 298                         cflag_out = rshift_cout;
 
 301                         if(!insn[4] && shift_amt[4:0] == 5'b0) begin /* RRX x.x */
 
 302                                 res = {cflag_in, operand[31:1]};
 
 303                                 cflag_out = operand[0];
 
 306                                 cflag_out = rshift_cout;
 
 312 module SuckLessShifter(
 
 318         output wire [31:0] res,
 
 322         wire [32:0] stage1, stage2, stage3, stage4, stage5;
 
 324         wire pushbits = is_arith & oper[31];
 
 326         /* do a barrel shift */
 
 327         assign stage1 = amt[5] ? {is_rot ? oper : {32{pushbits}}, oper[31]} : {oper, carryin};
 
 328         assign stage2 = amt[4] ? {is_rot ? stage1[16:1] : {16{pushbits}}, stage1[32:17], stage1[16]} : stage1;
 
 329         assign stage3 = amt[3] ? {is_rot ? stage2[8:1] : {8{pushbits}}, stage2[32:9], stage2[8]} : stage2;
 
 330         assign stage4 = amt[2] ? {is_rot ? stage3[4:1] : {4{pushbits}}, stage3[32:5], stage3[4]} : stage3;
 
 331         assign stage5 = amt[1] ? {is_rot ? stage4[2:1] : {2{pushbits}}, stage4[32:3], stage4[2]} : stage4;
 
 332         assign {res, carryout} = amt[0] ? {is_rot ? stage5[1] : pushbits, stage5[32:2], stage5[1]} : stage5;
 
 336 module SuckLessRotator(
 
 339         output wire [31:0] res
 
 342         wire [31:0] stage1, stage2, stage3;
 
 343         assign stage1 = amt[3] ? {oper[15:0], oper[31:16]} : oper;
 
 344         assign stage2 = amt[2] ? {stage1[7:0], stage1[31:8]} : stage1;
 
 345         assign stage3 = amt[1] ? {stage2[3:0], stage2[31:4]} : stage2;
 
 346         assign res    = amt[0] ? {stage3[1:0], stage3[31:2]} : stage3;