OpenCores
URL https://opencores.org/ocsvn/rf6809/rf6809/trunk

Subversion Repositories rf6809

[/] [rf6809/] [trunk/] [rtl/] [cpu/] [rf6809.sv] - Blame information for rev 22

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2022  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
//      rf6809.sv
9
//
10
//
11
// BSD 3-Clause License
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions are met:
14
//
15
// 1. Redistributions of source code must retain the above copyright notice, this
16
//    list of conditions and the following disclaimer.
17
//
18
// 2. Redistributions in binary form must reproduce the above copyright notice,
19
//    this list of conditions and the following disclaimer in the documentation
20
//    and/or other materials provided with the distribution.
21
//
22
// 3. Neither the name of the copyright holder nor the names of its
23
//    contributors may be used to endorse or promote products derived from
24
//    this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// ============================================================================
38
 
39
import rf6809_pkg::*;
40
 
41
module rf6809(id, rst_i, clk_i, halt_i, nmi_i, irq_i, firq_i, vec_i, ba_o, bs_o, lic_o, tsc_i,
42
        rty_i, bte_o, cti_o, bl_o, lock_o, cyc_o, stb_o, we_o, ack_i, aack_i, atag_i,
43
        adr_o, dat_i, dat_o, state);
44
parameter RESET = 6'd0;
45
parameter IFETCH = 6'd1;
46
parameter DECODE = 6'd2;
47
parameter CALC = 6'd3;
48
parameter PULL1 = 6'd4;
49
parameter PUSH1 = 6'd5;
50
parameter PUSH2 = 6'd6;
51
parameter LOAD1 = 6'd7;
52
parameter LOAD2 = 6'd8;
53
parameter STORE1 = 6'd9;
54 21 robfinch
parameter STORE1a = 6'd10;
55
parameter STORE2 = 6'd11;
56
parameter OUTER_INDEXING = 6'd12;
57
parameter OUTER_INDEXING2 = 6'd13;
58 22 robfinch
parameter DIV1 = 6'd16;
59
parameter DIV2 = 6'd17;
60
parameter DIV3 = 6'd18;
61
parameter MUL2 = 6'd20;
62 2 robfinch
parameter ICACHE1 = 6'd31;
63
parameter ICACHE2 = 6'd32;
64
parameter ICACHE3 = 6'd33;
65
parameter ICACHE4 = 6'd34;
66
parameter ICACHE5 = 6'd35;
67
parameter ICACHE6 = 6'd36;
68
parameter ICACHE7 = 6'd37;
69
parameter ICACHE8 = 6'd38;
70
parameter ICACHE9 = 6'd39;
71
parameter IBUF1 = 6'd40;
72
parameter IBUF2 = 6'd41;
73
parameter IBUF3 = 6'd42;
74
parameter IBUF4 = 6'd43;
75
parameter IBUF5 = 6'd44;
76
parameter IBUF6 = 6'd45;
77
input [5:0] id;
78
input rst_i;
79
input clk_i;
80
input halt_i;
81
input nmi_i;
82
input irq_i;
83
input firq_i;
84
input [`TRPBYTE] vec_i;
85
output reg ba_o;
86
output reg bs_o;
87
output lic_o;
88
input tsc_i;
89
input rty_i;
90
output reg [1:0] bte_o;
91
output reg [2:0] cti_o;
92
output reg [5:0] bl_o;
93
output reg cyc_o;
94
output reg stb_o;
95
output reg we_o;
96
output reg lock_o;
97
input ack_i;
98
input aack_i;
99
input [3:0] atag_i;
100
output reg [`TRPBYTE] adr_o;
101
input [`LOBYTE] dat_i;
102
output reg [`LOBYTE] dat_o;
103
output [5:0] state;
104
 
105
reg [5:0] state;
106
reg [5:0] load_what,store_what,load_what2;
107
reg [`TRPBYTE] pc;
108
wire [`TRPBYTE] pcp2 = pc + 4'd2;
109
wire [`TRPBYTE] pcp16 = pc + 5'd16;
110
wire [`HEXBYTE] insn;
111
wire icacheOn = 1'b1;
112
reg [`TRPBYTE] ibufadr, icwa;
113
reg [191:0] ibuf;
114
wire ibufhit = ibufadr==pc;
115 22 robfinch
reg natMd,firqMd,iplMd,dbz,iop;
116 2 robfinch
reg md32;
117
wire [`DBLBYTE] mask = 24'hFFFFFF;
118
reg [1:0] ipg;
119
reg isFar;
120
reg isOuterIndexed;
121
reg [`HEXBYTE] ir;
122
`ifdef EIGHTBIT
123
wire [9:0] ir12 = {ipg,ir[`LOBYTE]};
124
`endif
125
`ifdef TWELVEBIT
126
wire [`LOBYTE] ir12 = ir[`LOBYTE];
127
`endif
128
reg [`LOBYTE] dpr;              // direct page register
129
reg [`DBLBYTE] usppg;   // user stack pointer page
130 22 robfinch
 
131
Address [3:0] brkad;    // breakpoint addresses
132
brkCtrl [3:0] brkctrl;
133
 
134 2 robfinch
wire [`LOBYTE] ndxbyte;
135
reg cf,vf,zf,nf,hf,ef;
136
wire [`LOBYTE] cfx8 = cf;
137
wire [`DBLBYTE] cfx24 = {23'b0,cf};
138 22 robfinch
reg im,im1,firqim;
139
reg dm; // decimal mode
140 2 robfinch
reg sync_state,wait_state;
141 22 robfinch
wire [`LOBYTE] ccr = bitsPerByte==12 ? {2'b00,im1,dm,ef,firqim,hf,im,nf,zf,vf,cf} : {ef,firqim,hf,im,nf,zf,vf,cf};
142 2 robfinch
reg [`LOBYTE] acca,accb;
143 21 robfinch
`ifdef SUPPORT_6309
144
reg [`LOBYTE] acce,accf;
145
`endif
146 2 robfinch
reg [`DBLBYTE] accd;
147 21 robfinch
`ifdef SUPPORT_6309
148
reg [`DBLBYTE] accw;
149
`endif
150 2 robfinch
reg [`DBLBYTE] xr,yr,usp,ssp;
151
wire [`DBLBYTE] prod = acca * accb;
152
reg [`DBLBYTE] vect;
153
reg [`DBLBYTEP1] res;
154
reg [`LOBYTEP1] res12;
155
wire res12n = res12[BPBM1];
156
wire res12z = res12[`LOBYTE]==12'h000;
157
wire res12c = res12[bitsPerByte];
158
wire res24n = res[BPBX2M1];
159
wire res24z = res[`DBLBYTE]==24'h000000;
160
wire res24c = res[BPB*2];
161
reg [`TRPBYTE] ia;
162
reg ic_invalidate;
163
reg first_ifetch;
164
reg tsc_latched;
165
wire tsc = tsc_i|tsc_latched;
166
reg [`LOBYTE] chkpoint;
167
reg [15:0] icgot;
168
reg [23:0] btocnt;
169
reg bto;                                                        // bus timed out
170
 
171
reg [`DBLBYTE] a,b;
172
wire [`LOBYTE] b12 = b[`LOBYTE];
173
reg [`TRPBYTE] radr,wadr;
174
reg [`DBLBYTE] wdat;
175
 
176
reg nmi1,nmi_edge;
177
reg nmi_armed;
178
 
179
reg isStore;
180
reg isPULU,isPULS;
181
reg isPSHS,isPSHU;
182
reg isRTS,isRTI,isRTF;
183
reg isLEA;
184
reg isRMW;
185
 
186 21 robfinch
function fnAddOverflow;
187
input a;
188
input b;
189
input r;
190
begin
191
        fnAddOverflow = (r ^ b) & (1'b1 ^ a ^ b);
192
end
193
endfunction
194
 
195
function fnSubOverflow;
196
input a;
197
input b;
198
input r;
199
begin
200
        fnSubOverflow = (1'b1 ^ r ^ b) & (a ^ b);
201
end
202
endfunction
203
 
204 2 robfinch
// Data input path multiplexing
205 12 robfinch
reg [bitsPerByte-1:0] dati;
206 2 robfinch
always_comb
207
        dati = dat_i;
208
 
209 22 robfinch
genvar g;
210
 
211 2 robfinch
// Evaluate the branch conditional
212
reg takb;
213
always_comb
214
        case(ir12)
215
        `BRA,`LBRA:             takb <= 1'b1;
216
        `BRN,`LBRN:             takb <= 1'b0;
217
        `BHI,`LBHI:             takb <= !cf & !zf;
218
        `BLS,`LBLS:             takb <=  cf | zf;
219
        `BLO,`LBLO:             takb <=  cf;
220
        `BHS,`LBHS:             takb <= !cf;
221
        `BNE,`LBNE:             takb <= !zf;
222
        `BEQ,`LBEQ:             takb <=  zf;
223
        `BMI,`LBMI:             takb <=  nf;
224
        `BPL,`LBPL:             takb <= !nf;
225
        `BVS,`LBVS:             takb <=  vf;
226
        `BVC,`LBVC:             takb <= !vf;
227
        `BGT,`LBGT:             takb <= (nf & vf & !zf) | (!nf & !vf & !zf);
228
        `BGE,`LBGE:             takb <= (nf & vf) | (!nf & !vf);
229
        `BLE,`LBLE:             takb <= zf | (nf & !vf) | (!nf & vf);
230
        `BLT,`LBLT:             takb <= (nf & !vf) | (!nf & vf);
231
        default:        takb <= 1'b1;
232
        endcase
233
 
234
// This chunk of code takes care of calculating the number of bytes stacked
235
// by a push or pull operation.
236
//
237
reg [4:0] cnt;
238
always_comb
239
begin
240
        cnt =   (ir[bitsPerByte] ? 5'd1 : 5'd0) +
241
                        (ir[bitsPerByte+1] ? 5'd1 : 5'd0) +
242
                        (ir[bitsPerByte+2] ? 5'd1 : 5'd0) +
243
                        (ir[bitsPerByte+3] ? 5'd1 : 5'd0) +
244
                        (ir[bitsPerByte+4] ? 5'd2 : 5'd0) +
245
                        (ir[bitsPerByte+5] ? 5'd2 : 5'd0) +
246
                        (ir[bitsPerByte+6] ? 5'd2 : 5'd0) +
247 11 robfinch
                        (ir[bitsPerByte+7] ? (isFar ? 5'd3 : 5'd2) : 5'd0)
248 21 robfinch
`ifdef SUPPORT_6309
249
                        + (ir[bitsPerByte+8] ? 5'd1 : 5'd0) +
250
                        + (ir[bitsPerByte+9] ? 5'd1 : 5'd0)
251
`endif
252 2 robfinch
                        ;
253
//  cnt = 0;
254
//      if (ir[8]) cnt = cnt + 5'd1;    // CC
255
//      if (ir[9]) cnt = cnt + md32 ? 5'd4 : 5'd1;      // A
256
//      if (ir[10]) cnt = cnt + md32 ? 5'd4 : 5'd1;     // B
257
//      if (ir[BPBM1]) cnt = cnt + 5'd1;        // DP
258
//      if (ir[12]) cnt = cnt + md32 ? 5'd4 : 5'd2;     // X
259
//      if (ir[bitsPerByte+1]) cnt = cnt + md32 ? 5'd4 : 5'd2;  // Y
260
//      if (ir[bitsPerByte+2]) cnt = cnt + md32 ? 5'd4 : 5'd2;  // U/S
261
//      if (ir[bitsPerByte+3]) cnt = cnt + 5'd4;        // PC
262
end
263
 
264 21 robfinch
`ifdef SUPPORT_6309
265
wire isInMem =  ir12==`AIM_DP || ir12==`EIM_DP || ir12==`OIM_DP || ir12==`TIM_DP ||
266
                                ir12==`AIM_NDX || ir12==`EIM_NDX || ir12==`OIM_NDX || ir12==`TIM_NDX ||
267
                                ir12==`AIM_EXT || ir12==`EIM_EXT || ir12==`OIM_EXT || ir12==`TIM_EXT
268
                                ;
269
wire isRMW1 =   ir12==`AIM_DP || ir12==`EIM_DP || ir12==`OIM_DP ||
270
                                ir12==`NEG_DP || ir12==`COM_DP || ir12==`LSR_DP || ir12==`ROR_DP || ir12==`ASR_DP || ir12==`ASL_DP || ir12==`ROL_DP || ir12==`DEC_DP || ir12==`INC_DP ||
271
                                ir12==`AIM_NDX || ir12==`EIM_NDX || ir12==`OIM_NDX ||
272
                                ir12==`NEG_NDX || ir12==`COM_NDX || ir12==`LSR_NDX || ir12==`ROR_NDX || ir12==`ASR_NDX || ir12==`ASL_NDX || ir12==`ROL_NDX || ir12==`DEC_NDX || ir12==`INC_NDX ||
273
                                ir12==`AIM_EXT || ir12==`EIM_EXT || ir12==`OIM_EXT ||
274
                                ir12==`NEG_EXT || ir12==`COM_EXT || ir12==`LSR_EXT || ir12==`ROR_EXT || ir12==`ASR_EXT || ir12==`ASL_EXT || ir12==`ROL_EXT || ir12==`DEC_EXT || ir12==`INC_EXT
275
                                ;
276
`else
277
wire isInMem = 1'b0;
278 2 robfinch
wire isRMW1 =   ir12==`NEG_DP || ir12==`COM_DP || ir12==`LSR_DP || ir12==`ROR_DP || ir12==`ASR_DP || ir12==`ASL_DP || ir12==`ROL_DP || ir12==`DEC_DP || ir12==`INC_DP ||
279
                                ir12==`NEG_NDX || ir12==`COM_NDX || ir12==`LSR_NDX || ir12==`ROR_NDX || ir12==`ASR_NDX || ir12==`ASL_NDX || ir12==`ROL_NDX || ir12==`DEC_NDX || ir12==`INC_NDX ||
280
                                ir12==`NEG_EXT || ir12==`COM_EXT || ir12==`LSR_EXT || ir12==`ROR_EXT || ir12==`ASR_EXT || ir12==`ASL_EXT || ir12==`ROL_EXT || ir12==`DEC_EXT || ir12==`INC_EXT
281
                                ;
282 21 robfinch
`endif
283 2 robfinch
 
284
wire isIndexed =
285
        ir12[7:4]==4'h6 || ir12[7:4]==4'hA || ir12[7:4]==4'hE ||
286
        ir12==`LEAX_NDX || ir12==`LEAY_NDX || ir12==`LEAS_NDX || ir12==`LEAU_NDX
287
        ;
288
reg isDblIndirect;
289 12 robfinch
wire isIndirect = ndxbyte[bitsPerByte-4] & ndxbyte[bitsPerByte-1];
290 13 robfinch
`ifdef TWELVEBIT
291
always_comb
292
        isOuterIndexed = ndxbyte[bitsPerByte-5] & ndxbyte[bitsPerByte-1];
293
`endif
294
 
295 2 robfinch
assign ndxbyte = ir[`HIBYTE];
296
 
297
// Detect type of interrupt
298
wire isINT = ir12==`INT;
299
wire isRST = vect[3:0]==4'hE;
300
wire isNMI = vect[3:0]==4'hC;
301
wire isSWI = vect[3:0]==4'hA;
302
wire isIRQ = vect[3:0]==4'h8;
303
wire isFIRQ = vect[3:0]==4'h6;
304
wire isSWI2 = vect[3:0]==4'h4;
305
wire isSWI3 = vect[3:0]==4'h2;
306
 
307
wire [`TRPBYTE] far_address = {ir[`HIBYTE],ir[`BYTE3],ir[`BYTE4]};
308
wire [`TRPBYTE] address = {ir[`HIBYTE],ir[`BYTE3]};
309
wire [`TRPBYTE] dp_address = {dpr,ir[`HIBYTE]};
310
wire [`TRPBYTE] ex_address = isFar ? far_address : address;
311
wire [`TRPBYTE] offset12 = {{bitsPerByte{ir[bitsPerByte*3-1]}},ir[`BYTE3]};
312
wire [`TRPBYTE] offset24 = {ir[`BYTE3],ir[`BYTE4]};
313
wire [`TRPBYTE] offset36 = {ir[`BYTE3],ir[`BYTE4],ir[`BYTE5]};
314
 
315
// Choose the indexing register
316
reg [`TRPBYTE] ndxreg;
317
always_comb
318
        if (bitsPerByte==8)
319
                case(ndxbyte[6:5])
320
                2'b00:  ndxreg <= xr;
321
                2'b01:  ndxreg <= yr;
322
                2'b10:  ndxreg <= {usppg,8'h00} + usp;
323
                2'b11:  ndxreg <= ssp;
324
                endcase
325
        else if (bitsPerByte==12)
326
                case(ndxbyte[10:9])
327
                2'b00:  ndxreg <= xr;
328
                2'b01:  ndxreg <= yr;
329
                2'b10:  ndxreg <= {usppg,8'h00} + usp;
330
                2'b11:  ndxreg <= ssp;
331
                endcase
332
 
333
reg [`TRPBYTE] NdxAddr;
334
always_comb
335
        if (bitsPerByte==8)
336
                casez({isOuterIndexed,ndxbyte})
337
                9'b00???????:   NdxAddr <= ndxreg + {{19{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
338
                9'b01???0000:   NdxAddr <= ndxreg;
339
                9'b01???0001:   NdxAddr <= ndxreg;
340
                9'b01???0010:   NdxAddr <= ndxreg - 2'd1;
341
                9'b01???0011:   NdxAddr <= ndxreg - 2'd2;
342
                9'b01???0100:   NdxAddr <= ndxreg;
343
                9'b01???0101:   NdxAddr <= ndxreg + {{BPB*2{accb[BPBM1]}},accb};
344
                9'b01???0110:   NdxAddr <= ndxreg + {{BPB*2{acca[BPBM1]}},acca};
345
                9'b01???1000:   NdxAddr <= ndxreg + offset12;
346
                9'b01???1001:   NdxAddr <= ndxreg + offset24;
347
                9'b01???1010:   NdxAddr <= ndxreg + offset36;
348
                9'b01???1011:   NdxAddr <= ndxreg + {acca,accb};
349
                9'b01???1100:   NdxAddr <= pc + offset12 + 3'd3;
350
                9'b01???1101:   NdxAddr <= pc + offset24 + 3'd4;
351
                9'b01???1110:   NdxAddr <= pc + offset36 + 3'd5;
352
                9'b01??01111:   NdxAddr <= isFar ? offset36 : offset24;
353
                9'b01??11111:   NdxAddr <= offset24;
354
                9'b10???????:   NdxAddr <= {{11{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
355
                9'b11???0000:   NdxAddr <= 24'd0;
356
                9'b11???0001:   NdxAddr <= 24'd0;
357
                9'b11???0010:   NdxAddr <= 24'd0;
358
                9'b11???0011:   NdxAddr <= 24'd0;
359
                9'b11???0100:   NdxAddr <= 24'd0;
360
                9'b11???0101:   NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
361
                9'b11???0110:   NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
362
                9'b11???1000:   NdxAddr <= offset12;
363
                9'b11???1001:   NdxAddr <= offset24;
364
                9'b11???1010:   NdxAddr <= offset36;
365
                9'b11???1011:   NdxAddr <= {acca,accb};
366
                9'b11???1100:   NdxAddr <= pc + offset12 + 3'd3;
367
                9'b11???1101:   NdxAddr <= pc + offset24 + 3'd4;
368
                9'b11???1110:   NdxAddr <= pc + offset36 + 3'd5;
369
                9'b11??01111:   NdxAddr <= isFar ? offset36 : offset24;
370
                9'b11??11111:   NdxAddr <= offset24;
371
                default:                NdxAddr <= 24'hFFFFFF;
372
                endcase
373
        else if (bitsPerByte==12)
374
                casez({isOuterIndexed,ndxbyte})
375
                13'b00???????????:      NdxAddr <= ndxreg + {{27{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
376
                13'b01???00000000:      NdxAddr <= ndxreg;
377
                13'b01???00000001:      NdxAddr <= ndxreg;
378
                13'b01???00000010:      NdxAddr <= ndxreg - 2'd1;
379
                13'b01???00010010:      NdxAddr <= ndxreg - 2'd2;
380
                13'b01???00100010:      NdxAddr <= ndxreg - 2'd3;
381
                13'b01???00000011:      NdxAddr <= ndxreg - 2'd2;
382
                13'b01???00000100:      NdxAddr <= ndxreg;
383
                13'b01???00000101:      NdxAddr <= ndxreg + {{BPB*2{accb[BPBM1]}},accb};
384
                13'b01???00000110:      NdxAddr <= ndxreg + {{BPB*2{acca[BPBM1]}},acca};
385 22 robfinch
`ifdef SUPPORT_6309
386
                13'b01???00010101:      NdxAddr <= ndxreg + {{BPB*2{accf[BPBM1]}},accf};
387
                13'b01???00010110:      NdxAddr <= ndxreg + {{BPB*2{acce[BPBM1]}},acce};
388
                13'b01???00011011:      NdxAddr <= ndxreg + {acce,accf};
389
`endif
390 2 robfinch
                13'b01???00001000:      NdxAddr <= ndxreg + offset12;
391
                13'b01???00001001:      NdxAddr <= ndxreg + offset24;
392
                13'b01???00001010:      NdxAddr <= ndxreg + offset36;
393
                13'b01???00001011:      NdxAddr <= ndxreg + {acca,accb};
394
                13'b01???00001100:      NdxAddr <= pc + offset12 + 3'd3;
395
                13'b01???00001101:      NdxAddr <= pc + offset24 + 3'd4;
396
                13'b01???00001110:      NdxAddr <= pc + offset36 + 3'd5;
397
                13'b01??000001111:      NdxAddr <= isFar ? offset36 : offset24;
398
                13'b01??100001111:      NdxAddr <= offset24;
399
                13'b01???10000000:      NdxAddr <= 24'd0;
400
                13'b01???10000001:      NdxAddr <= 24'd0;
401
                13'b01???10000010:      NdxAddr <= 24'd0;
402
                13'b01???10000011:      NdxAddr <= 24'd0;
403
                13'b01???10000100:      NdxAddr <= 24'd0;
404
                13'b01???10000101:      NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
405
                13'b01???10000110:      NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
406 22 robfinch
`ifdef SUPPORT_6309
407
                13'b01???10010101:      NdxAddr <= {{BPB*2{accf[BPBM1]}},accf};
408
                13'b01???10010110:      NdxAddr <= {{BPB*2{acce[BPBM1]}},acce};
409
                13'b01???10011011:      NdxAddr <= {acce,accf};
410
`endif
411 2 robfinch
                13'b01???10001000:      NdxAddr <= offset12;
412
                13'b01???10001001:      NdxAddr <= offset24;
413 15 robfinch
                13'b01???10001010:      NdxAddr <= offset36;
414 2 robfinch
                13'b01???10001011:      NdxAddr <= {acca,accb};
415
                13'b01???10001100:      NdxAddr <= pc + offset12 + 3'd3;
416
                13'b01???10001101:      NdxAddr <= pc + offset24 + 3'd4;
417
                13'b01???10001110:      NdxAddr <= pc + offset36 + 3'd5;
418
                13'b01??010001111:      NdxAddr <= isFar ? offset36 : offset24;
419
                13'b01??110001111:      NdxAddr <= offset24;
420
                13'b10???????????:      NdxAddr <= {{15{ndxbyte[BPB-4]}},ndxbyte[BPB-4:0]};
421
                13'b11???00000000:      NdxAddr <= 24'd0;
422
                13'b11???00000001:      NdxAddr <= 24'd0;
423
                13'b11???00000010:      NdxAddr <= 24'd0;
424
                13'b11???00000011:      NdxAddr <= 24'd0;
425
                13'b11???00000100:      NdxAddr <= 24'd0;
426
                13'b11???00000101:      NdxAddr <= {{BPB*2{accb[BPBM1]}},accb};
427
                13'b11???00000110:      NdxAddr <= {{BPB*2{acca[BPBM1]}},acca};
428
                13'b11???00001000:      NdxAddr <= offset12;
429
                13'b11???00001001:      NdxAddr <= offset24;
430
                13'b11???00001010:      NdxAddr <= offset36;
431
                13'b11???00001011:      NdxAddr <= {acca,accb};
432
                13'b11???00001100:      NdxAddr <= pc + offset12 + 3'd3;
433
                13'b11???00001101:      NdxAddr <= pc + offset24 + 3'd4;
434
                13'b11???00001110:      NdxAddr <= pc + offset36 + 3'd5;
435
                13'b11??000001111:      NdxAddr <= isFar ? offset36 : offset24;
436
                13'b11??000011111:      NdxAddr <= offset24;
437
                default:                NdxAddr <= 24'hFFFFFF;
438
                endcase
439
 
440
// Compute instruction length depending on indexing byte
441
reg [2:0] insnsz;
442
always_comb
443
        if (bitsPerByte==8)
444
                casez(ndxbyte)
445
                8'b0???????:    insnsz <= 4'h2;
446
                8'b1??00000:    insnsz <= 4'h2;
447
                8'b1??00001:    insnsz <= 4'h2;
448
                8'b1??00010:    insnsz <= 4'h2;
449
                8'b1??00011:    insnsz <= 4'h2;
450
                8'b1??00100:    insnsz <= 4'h2;
451
                8'b1??00101:    insnsz <= 4'h2;
452
                8'b1??00110:    insnsz <= 4'h2;
453
                8'b1??01000:    insnsz <= 4'h3;
454
                8'b1??01001:    insnsz <= 4'h4;
455
                8'b1??01010:    insnsz <= 4'h5;
456
                8'b1??01011:    insnsz <= 4'h2;
457
                8'b1??01100:    insnsz <= 4'h3;
458
                8'b1??01101:    insnsz <= 4'h4;
459
                8'b1??01110:    insnsz <= 4'h5;
460
                8'b1??01111:    insnsz <= isFar ? 4'h5 : 4'h4;
461
                8'b1??11111:    insnsz <= 4'h4;
462
                default:        insnsz <= 4'h2;
463
                endcase
464
        else if (bitsPerByte==12)
465
                casez(ndxbyte)
466
                12'b0???????????:       insnsz <= 4'h2;
467
                12'b1???00000000:       insnsz <= 4'h2;
468
                12'b1???00000001:       insnsz <= 4'h2;
469
                12'b1???00000010:       insnsz <= 4'h2;
470
                12'b1???00000011:       insnsz <= 4'h2;
471
                12'b1???00000100:       insnsz <= 4'h2;
472 22 robfinch
                12'b1???000?0101:       insnsz <= 4'h2;
473
                12'b1???000?0110:       insnsz <= 4'h2;
474 2 robfinch
                12'b1???00001000:       insnsz <= 4'h3;
475
                12'b1???00001001:       insnsz <= 4'h4;
476
                12'b1???00001010:       insnsz <= 4'h5;
477 22 robfinch
                12'b1???000?1011:       insnsz <= 4'h2;
478 2 robfinch
                12'b1???00001100:       insnsz <= 4'h3;
479
                12'b1???00001101:       insnsz <= 4'h4;
480
                12'b1???00001110:       insnsz <= 4'h5;
481
                12'b1??000001111:       insnsz <= isFar ? 4'h5 : 4'h4;
482 15 robfinch
                12'b1??100001111:       insnsz <= 4'h4;
483 2 robfinch
                default:        insnsz <= 4'h2;
484
                endcase
485
 
486
// Source registers for transfer or exchange instructions.
487
reg [`DBLBYTE] src1,src2;
488
always_comb
489
        case(ir[bitsPerByte+7:bitsPerByte+4])
490
        4'b0000:        src1 <= {acca[`LOBYTE],accb[`LOBYTE]};
491
        4'b0001:        src1 <= xr;
492
        4'b0010:        src1 <= yr;
493
        4'b0011:        src1 <= usp;
494
        4'b0100:        src1 <= ssp;
495
        4'b0101:        src1 <= pcp2;
496 22 robfinch
        4'b1000:        src1 <= {12'hFFF,acca[`LOBYTE]};
497
        4'b1001:        src1 <= {12'hFFF,accb[`LOBYTE]};
498
        4'b1010:        src1 <= {ccr,ccr};
499
        4'b1011:        src1 <= {dpr,dpr};
500 2 robfinch
        4'b1100:        src1 <= usppg;
501
        4'b1101:        src1 <= 24'h0000;
502 21 robfinch
`ifdef SUPPORT_6309
503
        4'b0110:        src1 <= {acce[`LOBYTE],accf[`LOBYTE]};
504 22 robfinch
        4'b1110:        src1 <= {12'hFFF,acce};
505
        4'b1111:        src1 <= {12'hFFF,accf};
506 21 robfinch
`else
507 2 robfinch
        4'b1110:        src1 <= 24'h0000;
508
        4'b1111:        src1 <= 24'h0000;
509 21 robfinch
`endif
510 2 robfinch
        default:        src1 <= 24'h0000;
511
        endcase
512
always_comb
513
        case(ir[bitsPerByte+3:bitsPerByte])
514
        4'b0000:        src2 <= {acca[`LOBYTE],accb[`LOBYTE]};
515
        4'b0001:        src2 <= xr;
516
        4'b0010:        src2 <= yr;
517
        4'b0011:        src2 <= usp;
518
        4'b0100:        src2 <= ssp;
519
        4'b0101:        src2 <= pcp2;
520
        4'b1000:        src2 <= acca[`LOBYTE];
521
        4'b1001:        src2 <= accb[`LOBYTE];
522
        4'b1010:        src2 <= ccr;
523
        4'b1011:        src2 <= dpr;
524
        4'b1100:        src2 <= usppg;
525
        4'b1101:        src2 <= 24'h0000;
526 21 robfinch
`ifdef SUPPORT_6309
527
        4'b0110:        src2 <= {acce[`LOBYTE],accf[`LOBYTE]};
528
        4'b1110:        src2 <= acce;
529
        4'b1111:        src2 <= accf;
530
`else
531 2 robfinch
        4'b1110:        src2 <= 24'h0000;
532
        4'b1111:        src2 <= 24'h0000;
533 21 robfinch
`endif
534 2 robfinch
        default:        src2 <= 24'h0000;
535
        endcase
536
 
537 21 robfinch
wire [bitsPerByte*2:0] sum12 = src1 + src2;
538
wire [bitsPerByte*2:0] sum12c = src1 + src2 + cf;
539
wire [bitsPerByte*2-1:0] and12 = src1 & src2;
540
wire [bitsPerByte*2-1:0] eor12 = src1 ^ src2;
541
wire [bitsPerByte*2-1:0] or12 = src1 | src2;
542
wire [bitsPerByte*2:0] dif12 = src1 - src2;
543
wire [bitsPerByte*2:0] dif12c = src1 - src2 - cf;
544
 
545 2 robfinch
wire isAcca     =       ir12==`NEGA || ir12==`COMA || ir12==`LSRA || ir12==`RORA || ir12==`ASRA || ir12==`ASLA ||
546
                                ir12==`ROLA || ir12==`DECA || ir12==`INCA || ir12==`TSTA || ir12==`CLRA ||
547
                                ir12==`SUBA_IMM || ir12==`CMPA_IMM || ir12==`SBCA_IMM || ir12==`ANDA_IMM || ir12==`BITA_IMM ||
548
                                ir12==`LDA_IMM || ir12==`EORA_IMM || ir12==`ADCA_IMM || ir12==`ORA_IMM || ir12==`ADDA_IMM ||
549
                                ir12==`SUBA_DP || ir12==`CMPA_DP || ir12==`SBCA_DP || ir12==`ANDA_DP || ir12==`BITA_DP ||
550
                                ir12==`LDA_DP || ir12==`EORA_DP || ir12==`ADCA_DP || ir12==`ORA_DP || ir12==`ADDA_DP ||
551
                                ir12==`SUBA_NDX || ir12==`CMPA_NDX || ir12==`SBCA_NDX || ir12==`ANDA_NDX || ir12==`BITA_NDX ||
552
                                ir12==`LDA_NDX || ir12==`EORA_NDX || ir12==`ADCA_NDX || ir12==`ORA_NDX || ir12==`ADDA_NDX ||
553
                                ir12==`SUBA_EXT || ir12==`CMPA_EXT || ir12==`SBCA_EXT || ir12==`ANDA_EXT || ir12==`BITA_EXT ||
554
                                ir12==`LDA_EXT || ir12==`EORA_EXT || ir12==`ADCA_EXT || ir12==`ORA_EXT || ir12==`ADDA_EXT
555
                                ;
556 22 robfinch
wire isAdc =    ir12==`ADCA_IMM || ir12==`ADCA_DP || ir12==`ADCA_NDX || ir12==`ADCA_EXT ||
557
                                                        ir12==`ADCB_IMM || ir12==`ADCB_DP || ir12==`ADCB_NDX || ir12==`ADCB_EXT ||
558
                                                        ir12==`ADCD_IMM || ir12==`ADCD_DP || ir12==`ADCD_NDX || ir12==`ADCD_EXT ;
559
wire isSbc =    ir12==`SBCA_IMM || ir12==`SBCA_DP || ir12==`SBCA_NDX || ir12==`SBCA_EXT ||
560
                                                        ir12==`SBCB_IMM || ir12==`SBCB_DP || ir12==`SBCB_NDX || ir12==`SBCB_EXT ||
561
                                                        ir12==`SBCD_IMM || ir12==`SBCD_DP || ir12==`SBCD_NDX || ir12==`SBCD_EXT ;
562 2 robfinch
 
563 21 robfinch
`ifdef SUPPORT_6309
564
wire isAcce =   ir12 == `ADDE_IMM || ir12==`ADDE_DP || ir12==`ADDE_NDX || ir12==`ADDE_EXT || ir12==`CLRE || ir12==`COME ||
565
                                                                ir12 == `SUBE_IMM || ir12==`SUBE_DP || ir12==`SUBE_NDX || ir12==`SUBE_EXT ||
566
                                                                ir12 == `LDE_IMM || ir12==`LDE_DP || ir12==`LDE_NDX || ir12==`LDE_EXT ||
567
                                                                ir12 == `DECE || ir12==`INCE ||
568
                                                                ir12 == `CMPE_IMM || ir12==`CMPE_DP || ir12==`CMPE_NDX || ir12==`CMPE_EXT
569
                                ;
570
wire isAccf =   ir12 == `ADDF_IMM || ir12==`ADDF_DP || ir12==`ADDF_NDX || ir12==`ADDF_EXT || ir12==`CLRF || ir12==`COMF ||
571
                                                                ir12 == `SUBF_IMM || ir12==`SUBF_DP || ir12==`SUBF_NDX || ir12==`SUBF_EXT ||
572
                                                                ir12 == `LDF_IMM || ir12==`LDF_DP || ir12==`LDF_NDX || ir12==`LDF_EXT ||
573
                                                                ir12 == `DECF || ir12==`INCF ||
574
                                                                ir12 == `CMPF_IMM || ir12==`CMPF_DP || ir12==`CMPF_NDX || ir12==`CMPF_EXT
575
                                ;
576
wire [`DBLBYTE] acc = isAcce ? acce : isAccf ? accf : isAcca ? acca : accb;
577
`else
578 2 robfinch
wire [`DBLBYTE] acc = isAcca ? acca : accb;
579 21 robfinch
`endif
580 2 robfinch
 
581
always_ff @(posedge clk_i)
582
if (state==DECODE) begin
583
        isStore <=      ir12==`STA_DP || ir12==`STB_DP || ir12==`STD_DP || ir12==`STX_DP || ir12==`STY_DP || ir12==`STU_DP || ir12==`STS_DP ||
584
                                ir12==`STA_NDX || ir12==`STB_NDX || ir12==`STD_NDX || ir12==`STX_NDX || ir12==`STY_NDX || ir12==`STU_NDX || ir12==`STS_NDX ||
585 21 robfinch
                                ir12==`STA_EXT || ir12==`STB_EXT || ir12==`STD_EXT || ir12==`STX_EXT || ir12==`STY_EXT || ir12==`STU_EXT || ir12==`STS_EXT ||
586
                                ir12==`STE_DP || ir12==`STE_NDX || ir12==`STE_EXT || ir12==`STF_DP || ir12==`STF_NDX || ir12==`STF_EXT ||
587
                                ir12==`STW_DP || ir12==`STW_NDX || ir12==`STW_EXT
588 2 robfinch
                                ;
589
        isPULU <= ir12==`PULU;
590
        isPULS <= ir12==`PULS;
591
        isPSHS <= ir12==`PSHS;
592
        isPSHU <= ir12==`PSHU;
593
        isRTI <= ir12==`RTI;
594
        isRTS <= ir12==`RTS;
595
        isRTF <= ir12==`RTF;
596
        isLEA <= ir12==`LEAX_NDX || ir12==`LEAY_NDX || ir12==`LEAU_NDX || ir12==`LEAS_NDX;
597
        isRMW <= isRMW1;
598
end
599
 
600
wire hit0, hit1;
601
wire ihit = hit0 & hit1;
602
reg rhit0;
603
 
604
assign lic_o =  (state==CALC && !isRMW) ||
605
                                (state==DECODE && (
606 22 robfinch
                                        ir12==`NOP || ir12==`ORCC || ir12==`ANDCC || ir12==`DAA || ir12==`LDMD || ir12==`BITMD || ir12==`TFR || ir12==`EXG ||
607 2 robfinch
                                        ir12==`NEGA || ir12==`COMA || ir12==`LSRA || ir12==`RORA || ir12==`ASRA || ir12==`ROLA || ir12==`DECA || ir12==`INCA || ir12==`TSTA || ir12==`CLRA ||
608 21 robfinch
                                        ir12==`DECE || ir12==`DECF || ir12==`DECD || ir12==`DECW || ir12==`INCE || ir12==`INCF || ir12==`INCD || ir12==`INCW ||
609 2 robfinch
                                        ir12==`NEGB || ir12==`COMB || ir12==`LSRB || ir12==`RORB || ir12==`ASRB || ir12==`ROLB || ir12==`DECB || ir12==`INCB || ir12==`TSTB || ir12==`CLRB ||
610 21 robfinch
                                        ir12==`COME || ir12==`COMF || ir12==`COMD || ir12==`COMW ||
611
                                        ir12==`ASLD || ir12==`ASRD || ir12==`TSTD || ir12==`ADDR || ir12==`ADCR || ir12==`ANDR ||
612
                                        ir12==`TSTE || ir12==`TSTF || ir12==`TSTW ||
613
                                        ir12==`LSRD || ir12==`LSRW || ir12==`NEGD || ir12==`ROLD || ir12==`ROLW || ir12==`RORD || ir12==`RORW ||
614 2 robfinch
                                        ir12==`SUBA_IMM || ir12==`CMPA_IMM || ir12==`SBCA_IMM || ir12==`ANDA_IMM || ir12==`BITA_IMM || ir12==`LDA_IMM || ir12==`EORA_IMM || ir12==`ADCA_IMM || ir12==`ORA_IMM || ir12==`ADDA_IMM ||
615
                                        ir12==`SUBB_IMM || ir12==`CMPB_IMM || ir12==`SBCB_IMM || ir12==`ANDB_IMM || ir12==`BITB_IMM || ir12==`LDB_IMM || ir12==`EORB_IMM || ir12==`ADCB_IMM || ir12==`ORB_IMM || ir12==`ADDB_IMM ||
616 21 robfinch
                                        ir12==`EORD_IMM || ir12==`ANDD_IMM || ir12==`ORD_IMM || ir12==`BITD_IMM || ir12==`ADDD_IMM || ir12==`ADCD_IMM || ir12==`SUBD_IMM || ir12==`SBCD_IMM || ir12==`LDD_IMM || ir12==`LDW_IMM ||
617
                                        ir12==`LDQ_IMM || ir12==`CMPD_IMM || ir12==`CMPX_IMM || ir12==`CMPY_IMM || ir12==`CMPU_IMM || ir12==`CMPS_IMM || ir12==`CMPW_IMM ||
618
                                        ir12==`LDE_IMM || ir12==`LDF_IMM ||
619
                                        ir12==`SUBE_IMM || ir12==`SUBF_IMM || ir12==`SUBW_IMM ||
620 2 robfinch
                                        ir12==`BEQ || ir12==`BNE || ir12==`BMI || ir12==`BPL || ir12==`BVS || ir12==`BVC || ir12==`BRA || ir12==`BRN ||
621
                                        ir12==`BHI || ir12==`BLS || ir12==`BHS || ir12==`BLO ||
622
                                        ir12==`BGT || ir12==`BGE || ir12==`BLT || ir12==`BLE ||
623
                                        ir12==`LBEQ || ir12==`LBNE || ir12==`LBMI || ir12==`LBPL || ir12==`LBVS || ir12==`LBVC || ir12==`LBRA || ir12==`LBRN ||
624
                                        ir12==`LBHI || ir12==`LBLS || ir12==`LBHS || ir12==`LBLO ||
625
                                        ir12==`LBGT || ir12==`LBGE || ir12==`LBLT || ir12==`LBLE
626
                                        )
627
                                ) ||
628
                                (state==STORE2 && (
629
                                        (store_what==`SW_ACCQ3124 && wadr[1:0]==2'b00) ||
630
                                        (store_what==`SW_ACCQ70) ||
631
                                        (store_what==`SW_ACCA && !(isINT || isPSHS || isPSHU)) ||
632
                                        (store_what==`SW_ACCB && !(isINT || isPSHS || isPSHU)) ||
633
                                        (store_what==`SW_ACCDH && wadr[1:0]!=2'b11) ||
634
                                        (store_what==`SW_ACCDL) ||
635
                                        (store_what==`SW_X3124 && wadr[1:0]==2'b00 && !(isINT || isPSHS || isPSHU)) ||
636
                                        (store_what==`SW_XL && !(isINT || isPSHS || isPSHU)) ||
637
                                        (store_what==`SW_YL && !(isINT || isPSHS || isPSHU)) ||
638
                                        (store_what==`SW_USPL && !(isINT || isPSHS || isPSHU)) ||
639
                                        (store_what==`SW_SSPL && !(isINT || isPSHS || isPSHU)) ||
640
                                        (store_what==`SW_PCL && !(isINT || isPSHS || isPSHU) && !(ir12==`JSR_NDX && isIndirect)) ||
641
                                        (store_what==`SW_ACCA70 && !(isINT || isPSHS || isPSHU)) ||
642
                                        (store_what==`SW_ACCB70 && !(isINT || isPSHS || isPSHU))
643
                                )) ||
644
                                (state==PUSH2 && ir[`HIBYTE]==12'h000 && !isINT) ||
645
                                (state==PULL1 && ir[`HIBYTE]==12'h000) ||
646
                                (state==OUTER_INDEXING2 && isLEA) ||
647
                                (state==LOAD2 &&
648
                                        (load_what==`LW_ACCA && !(isRTI || isPULU || isPULS)) ||
649
                                        (load_what==`LW_ACCB && !(isRTI || isPULU || isPULS)) ||
650
                                        (load_what==`LW_DPR && !(isRTI || isPULU || isPULS)) ||
651
                                        (load_what==`LW_XL && !(isRTI || isPULU || isPULS)) ||
652
                                        (load_what==`LW_YL && !(isRTI || isPULU || isPULS)) ||
653
                                        (load_what==`LW_USPL && !(isRTI || isPULU || isPULS)) ||
654
                                        (load_what==`LW_SSPL && !(isRTI || isPULU || isPULS)) ||
655
                                        (load_what==`LW_PCL) ||
656
                                        (load_what==`LW_IAL && !isOuterIndexed && isLEA) ||
657
                                        (load_what==`LW_IA3124 && radr[1:0]==2'b00 && !isOuterIndexed && isLEA)
658
                                )
659
                                ;
660
 
661
wire lock_bus = load_what==`LW_XH || load_what==`LW_YH || load_what==`LW_USPH || load_what==`LW_SSPH ||
662 21 robfinch
                                load_what==`LW_PCH || load_what==`LW_BH || load_what==`LW_IAH || load_what==`LW_PC2316 ||
663
                                load_what==`LW_IA2316 || load_what==`LW_B2316 ||
664
                                load_what==`LW_X2316 || load_what==`LW_Y2316 || load_what==`LW_USP2316 || load_what==`LW_SSP2316 ||
665 2 robfinch
                                isRMW ||
666
                                store_what==`SW_ACCDH || store_what==`SW_XH || store_what==`SW_YH || store_what==`SW_USPH || store_what==`SW_SSPH ||
667 21 robfinch
                                store_what==`SW_PCH || store_what==`SW_PC2316 || store_what==`SW_ACCQ2316 ||
668
                                store_what==`SW_X2316 || store_what==`SW_Y2316 || store_what==`SW_USP2316 || store_what==`SW_SSP2316
669 2 robfinch
                                ;
670
 
671
wire isPrefix = ir12==`PG2 || ir12==`PG3 || ir12==`OUTER;
672
 
673
reg rty;
674
reg [5:0] waitcnt;
675
reg [3:0] iccnt;
676
reg [bitsPerByte-1:0] icbuf [0:15];
677
reg [bitsPerByte*16-1:0] icbuf2;
678
reg [15:0] outstanding; // Outstanding async read cycles.
679
integer n4;
680
 
681
rf6809_icachemem u1
682
(
683
        .wclk(clk_i),
684
        .wce(1'b1),
685
        .wr(state==ICACHE6),
686
        .wa(icwa[11:0]),
687
        .i(icbuf2),
688
        .rclk(~clk_i),
689
        .rce(1'b1),
690
        .pc(pc[11:0]),
691
        .insn(insn)
692
);
693
 
694
rf6809_itagmem u2
695
(
696
        .wclk(clk_i),
697
        .wce(1'b1),
698
        .wr(state==ICACHE6),
699
        .wa(icwa[`TRPBYTE]),
700
        .invalidate(ic_invalidate),
701
        .rclk(~clk_i),
702
        .rce(1'b1),
703
        .pc(pc),
704
        .hit0(hit0),
705
        .hit1(hit1)
706
);
707
 
708 22 robfinch
wire bcdaddbcf, bcdsuubbcf,bcdaddcf,bcdsubcf,bcdnegcf,bcdnegbcf;
709
wire [bitsPerByte-1:0] bcdaddbo, bcdsubbo, bcdnegbo;
710
wire [bitsPerByte*2-1:0] bcdaddo, bcdsubo, bcdnego;
711
wire [31:0] bcdmulo;
712
 
713
`ifdef SUPPORT_BCD
714
BCDAddN #(.N(3)) ubcda1 (
715
        .ci(isAdc ? cf : 1'b0),
716
        .a(acc),
717
        .b(b12),
718
        .o(bcdaddbo),
719
        .co(bcdaddbcf)
720
);
721
 
722
BCDAddN #(.N(6)) ubcda2 (
723
        .ci(isAdc ? cf : 1'b0),
724
        .a({acca,accb}),
725
        .b(b),
726
        .o(bcdaddo),
727
        .co(bcdaddcf)
728
);
729
 
730
BCDSubN #(.N(3)) ubcds1 (
731
        .ci(isSbc ? cf : 1'b0),
732
        .a(acc),
733
        .b(b12),
734
        .o(bcdsubbo),
735
        .co(bcdsubbcf)
736
);
737
 
738
BCDSubN #(.N(6)) ubcds2 (
739
        .ci(isSbc ? cf : 1'b0),
740
        .a({acca,accb}),
741
        .b(b),
742
        .o(bcdsubo),
743
        .co(bcdsubcf)
744
);
745
 
746
BCDSubN #(.N(3)) ubcds3 (
747
        .ci(1'b0),
748
        .a(12'h0),
749
        .b(acc),
750
        .o(bcdnegbo),
751
        .co(bcdnegbcf)
752
);
753
 
754
BCDSubN #(.N(6)) ubcds4 (
755
        .ci(1'b0),
756
        .a(12'h0),
757
        .b({acca,accb}),
758
        .o(bcdnego),
759
        .co(bcdnegcf)
760
);
761
 
762
BCDMul4 ubcdmul1
763
(
764
        .a({4'h0,acca}),
765
        .b({4'h0,accb}),
766
        .o(bcdmulo)
767
);
768
`endif
769
 
770
reg [bitsPerByte*2-1:0] bcdmul_res [0:15];
771
reg [bitsPerByte*2-1:0] bcdmul_res16;
772
genvar g5;
773
generate begin : gBCDMulPipe
774
        always_ff @(posedge clk_i)
775
                bcdmul_res[0] <= bcdmulo[23:0];
776
        always_ff @(posedge clk_i)
777
                bcdmul_res16 <= muld_res[15];
778
        for (g5 = 1; g5 < 16; g5 = g5 + 1)
779
                always_ff @(posedge clk_i)
780
                        bcdmul_res[g5] = bcdmul_res[g5-1];
781
end
782
endgenerate
783
 
784
// Multiplier logic
785
wire signed [`QUADBYTE] muld_prod = $signed({acca,accb}) * $signed(b[`DBLBYTE]);
786
reg [`QUADBYTE] muld_res [0:15];
787
reg [`QUADBYTE] muld_res6;
788
genvar g4;
789
generate begin : gMulPipe
790
        always_ff @(posedge clk_i)
791
                muld_res[0] <= muld_prod;
792
        always_ff @(posedge clk_i)
793
                muld_res6 <= muld_res[5];
794
        for (g4 = 1; g4 < 6; g4 = g4 + 1)
795
                always_ff @(posedge clk_i)
796
                        muld_res[g4] = muld_res[g4-1];
797
end
798
endgenerate
799
 
800
// Divider logic
801
reg [5:0] divcnt;
802
/*
803
reg divsign;
804
reg [`DBLBYTE] dividend;
805
// Table of positive constants 1/0 to 1/2047, accurate to 35 bits
806
reg [26:0] divtbl [0:2047];
807
genvar g2;
808 21 robfinch
generate begin: gDivtbl
809 22 robfinch
        for (g2 = 0; g2 < 2048; g2 = g2 + 1)
810
        initial begin
811
                divtbl[g2] = 27'h4000000 / g2;
812
        end
813
end
814 21 robfinch
endgenerate
815 22 robfinch
reg [49:0] divres;
816
always_comb
817
        divres = ({36'd0,dividend} * divtbl[b12]);
818
reg [11:0] divrem;
819
always_comb
820
        divrem = dividend - divres[49:26] * b12;
821
// Now create an 12-stage divider pipeline. Hopefully the synthesizer
822
// will backfill along this pipeline. Each multiplier requires only
823
// about 5 stages for best performance.
824
genvar g1;
825
reg [49:0] divrespipe [0:31];
826
reg [11:0] divrempipe [0:31];
827
reg [49:0] divres12;
828
reg [11:0] divrem12;
829
generate begin : gDivPipe
830
        always_ff @(posedge clk_i)
831
                divrespipe[0] <= divres;
832
        always_ff @(posedge clk_i)
833
                divrempipe[0] <= divrem;
834
        always_ff @(posedge clk_i)
835
                divres12 <= divrespipe[12];
836
        always_ff @(posedge clk_i)
837
                divrem12 <= divrempipe[12];
838
        for (g1 = 1; g1 < 13; g1 = g1 + 1)
839
        always_ff @(posedge clk_i) begin
840
                divrespipe[g1] <= divrespipe[g1-1];
841
                divrempipe[g1] <= divrempipe[g1-1];
842
        end
843
end
844
endgenerate
845 21 robfinch
*/
846 22 robfinch
wire [23:0] divres24;
847
wire [15:0] divrem12;
848
wire [47:0] divres48;
849
wire [23:0] divrem24;
850
wire [15:0] divres16;
851
wire [7:0] divrem8;
852
wire [31:0] divres32;
853
wire [15:0] divrem16;
854 21 robfinch
 
855 22 robfinch
`ifdef SUPPORT_6309
856
`ifdef SUPPORT_DIVIDE
857
generate begin : gDividers
858
        if (bitsPerByte==12) begin
859
                div24by12 udiv24by12 (
860
                  .aclk(clk_i),                                      // input wire aclk
861
                  .s_axis_divisor_tvalid(1'b1),    // input wire s_axis_divisor_tvalid
862
                  .s_axis_divisor_tdata({4'h0,b12}),      // input wire [15 : 0] s_axis_divisor_tdata
863
                  .s_axis_dividend_tvalid(1'b1),  // input wire s_axis_dividend_tvalid
864
                  .s_axis_dividend_tdata({acca,accb}),    // input wire [23 : 0] s_axis_dividend_tdata
865
                  .m_axis_dout_tvalid(),          // output wire m_axis_dout_tvalid
866
                  .m_axis_dout_tuser(),            // output wire [0 : 0] m_axis_dout_tuser
867
                  .m_axis_dout_tdata({divres24,divrem12})            // output wire [39 : 0] m_axis_dout_tdata
868
                );
869
 
870
                div48by24 udiv48by24 (
871
                  .aclk(clk_i),                                      // input wire aclk
872
                  .s_axis_divisor_tvalid(1'b1),    // input wire s_axis_divisor_tvalid
873
                  .s_axis_divisor_tdata(b),      // input wire [23 : 0] s_axis_divisor_tdata
874
                  .s_axis_dividend_tvalid(1'b1),  // input wire s_axis_dividend_tvalid
875
                  .s_axis_dividend_tdata({acca,accb,acce,accf}),    // input wire [47 : 0] s_axis_dividend_tdata
876
                  .m_axis_dout_tvalid(),          // output wire m_axis_dout_tvalid
877
                  .m_axis_dout_tuser(),            // output wire [0 : 0] m_axis_dout_tuser
878
                  .m_axis_dout_tdata({divres48,divrem24})            // output wire [71 : 0] m_axis_dout_tdata
879
                );
880
        end
881
end
882
endgenerate
883
`endif
884
`endif
885
 
886 2 robfinch
// For asynchronous reads,
887
// The read response might come back in any order (the packets could loop
888
// around in the network.
889
// We need to buffer and reorder the response correctly.
890
 
891
integer n3;
892
always_ff @(posedge clk_i)
893
if (rst_i) begin
894
        icgot <= 16'h0;
895
        for (n3 = 0; n3 < 16; n3 = n3 + 1)
896
                icbuf[n3] <= {bitsPerByte{1'b0}};
897
end
898
else begin
899
        if (state==ICACHE1)
900
                icgot <= 16'h0;
901
`ifdef SUPPORT_AREAD
902
        if (aack_i) begin
903
                icgot[atag_i] <= 1'b1;
904
                icbuf[atag_i] <= dati;
905
        end
906
`else
907
        if (ack_i) begin
908
                icgot[adr_o[3:0]] <= 1'b1;
909
                icbuf[adr_o[3:0]] <= dati;
910
        end
911
`endif
912
end
913
 
914 22 robfinch
genvar g3;
915 2 robfinch
generate begin : gIcin
916 22 robfinch
for (g3 = 0; g3 < 16; g3 = g3 + 1)
917 2 robfinch
        always_comb
918 22 robfinch
                icbuf2[(g3+1)*bitsPerByte-1:g3*bitsPerByte] <= icbuf[g3];
919 2 robfinch
end
920
endgenerate
921
 
922
// Bus timeout counter
923
always_ff @(posedge clk_i)
924
if (rst_i) begin
925
        btocnt <= 24'd0;
926
end
927
else begin
928
        if (cyc_o & stb_o)
929
                btocnt <= btocnt + 2'd1;
930
        else
931
                btocnt <= 24'd0;
932
end
933
always_comb
934
        bto = btocnt >= 24'd10000;
935
 
936
// Count  milliseconds
937
// Based on a count determined by the clock frequency
938
// 40MHz is assumed.
939
reg [23:0] ns_count;    // The counter to get to 1ms
940
reg [35:0] ms_count;    // Count of number of milliseconds
941
 
942
always_ff @(posedge clk_i)
943
if (rst_i) begin
944
        ns_count <= 16'd0;
945
        ms_count <= 36'd0;
946
end
947
else begin
948
        ns_count <= ns_count + 2'd1;
949
        if (ns_count>=24'd40000) begin
950
                ns_count <= 24'h0;
951
                ms_count <= ms_count + 2'd1;
952
        end
953
end
954
 
955
`ifdef SUPPORT_CHECKPOINT
956
always_ff @(posedge clk_i)
957
if (rst_i)
958
        chkpoint <= 12'h000;
959
else begin
960
        if (ns_count==16'd40000) begin
961
                if (ms_count[9:0]==10'h3FF)
962
                        chkpoint <= 12'hFFF;
963
        end
964
        if (state==STORE1 && (wadr=={{BPB*3-8{1'b1}},8'hE1}))
965
                chkpoint <= 12'h000;
966
end
967
`endif
968
 
969
always_ff @(posedge clk_i)
970
        tsc_latched <= tsc_i;
971
 
972
always_ff @(posedge clk_i)
973 22 robfinch
        nmi1 <= iplMd ? &{nmi_i,firq_i,irq_i} : nmi_i;
974 2 robfinch
always_ff @(posedge clk_i)
975
`ifdef SUPPORT_CHECKPOINT
976
        if (ms_count[9:0]==10'h3FF && chkpoint!=12'h000)
977
                nmi_edge <= 1'b1;
978
        else
979
`endif
980 22 robfinch
        if ((iplMd ? &{nmi_i,firq_i,irq_i} : nmi_i) & !nmi1)
981 2 robfinch
                nmi_edge <= 1'b1;
982
        else if (state==DECODE && ir12==`INT)
983
                nmi_edge <= 1'b0;
984
 
985 21 robfinch
reg [11:0] rst_cnt;
986 2 robfinch
 
987
always @(posedge clk_i)
988
if (rst_i) begin
989
        wb_nack();
990 22 robfinch
        natMd <= 1'b0;
991
        firqMd <= 1'b0;
992
        iplMd <= 1'b0;
993 2 robfinch
        rty <= `FALSE;
994
        rst_cnt <= {id,4'd0};
995
        next_state(RESET);
996
        sync_state <= `FALSE;
997
        wait_state <= `FALSE;
998
        md32 <= `FALSE;
999
        ipg <= 2'b00;
1000
        isFar <= `FALSE;
1001 13 robfinch
`ifdef EIGHTBIT
1002 2 robfinch
        isOuterIndexed <= `FALSE;
1003 13 robfinch
`endif
1004 2 robfinch
        dpr <= 12'h000;
1005
        ibufadr <= {BPB*3{1'b0}};
1006
//      pc <= 24'hFFFFFE;
1007
        pc <= {{BPB*3-1{1'b1}},1'b0};   // FF...FE
1008
        ir <= {4{`NOP}};
1009
        ibuf <= {4{`NOP}};
1010 22 robfinch
        dm <= 1'b0;
1011 2 robfinch
        im <= 1'b1;
1012
        firqim <= 1'b1;
1013
        nmi_armed <= `FALSE;
1014
        ic_invalidate <= `TRUE;
1015
        first_ifetch <= `TRUE;
1016
        acca <= 12'h0;
1017
        accb <= 12'h0;
1018
        accd <= 24'h0;
1019 22 robfinch
`ifdef SUPPORT_6309
1020
        accw <= 24'h0;
1021
`endif
1022 2 robfinch
        xr <= 24'h0;
1023
        yr <= 24'h0;
1024 15 robfinch
        usppg <= 16'h0;
1025 2 robfinch
        usp <= 24'h0;
1026
        ssp <= 24'h0;
1027
        if (halt_i) begin
1028
                ba_o <= 1'b1;
1029
                bs_o <= 1'b1;
1030
        end
1031
        else begin
1032
                ba_o <= 1'b0;
1033
                bs_o <= 1'b0;
1034
        end
1035
        outstanding <= 16'h0;
1036
        iccnt <= 4'h0;
1037 22 robfinch
        //dividend <= 'b0;
1038
        divcnt <= 'b0;
1039
        //divsign <= 'b0;
1040 2 robfinch
end
1041
else begin
1042
 
1043
// Release any bus lock during the last state of an instruction.
1044
if (lic_o && ack_i && (state==STORE2 || state==LOAD2))
1045
        lock_o <= 1'b0;
1046
 
1047
case(state)
1048
RESET:
1049
        if (rst_cnt==10'd0) begin
1050
                ic_invalidate <= `FALSE;
1051
                ba_o <= 1'b0;
1052
                bs_o <= 1'b0;
1053
                vect <= `RST_VECT;
1054
                radr <= `RST_VECT;
1055
                load_what <= `LW_PCH;
1056
                next_state(LOAD1);
1057
        end
1058
        else
1059
                rst_cnt <= rst_cnt - 2'd1;
1060
 
1061
IFETCH:
1062
        begin
1063
                tIfetch();
1064
                tWriteback();
1065
        end
1066
DECODE: tDecode();
1067
LOAD1:  tLoad1();
1068
LOAD2:  tLoad2();
1069
CALC:           tExecute();
1070
STORE1: tStore1();
1071 21 robfinch
STORE1a:        tStore1a();
1072 2 robfinch
STORE2: tStore2();
1073
 
1074
// ============================================================================
1075
// ============================================================================
1076 22 robfinch
MUL2:
1077
        if (divcnt != 6'd0)
1078
                divcnt <= divcnt - 2'd1;
1079
        else
1080
                next_state(IFETCH);
1081
DIV1:
1082
        begin
1083
                /*
1084
                divsign <= acca[bitsPerByte-1] ^ b12[bitsPerByte-1];
1085
                if (acca[bitsPerByte-1])
1086
                        dividend <= -{acca,accb};
1087
                else
1088
                        dividend <= {acca,accb};
1089
                if (b12[bitsPerByte-1])
1090
                        b <= -b;
1091
                */
1092
                case(ir12)
1093
                `DIVD_IMM,`DIVD_DP,`DIVD_NDX,`DIVD_EXT:
1094
                        divcnt <= 6'd28;
1095
                `DIVQ_IMM,`DIVQ_DP,`DIVQ_NDX,`DIVQ_EXT:
1096
                        divcnt <= 6'd52;
1097
                endcase
1098
                next_state(DIV2);
1099
        end
1100
DIV2:
1101
        if (divcnt != 6'd0)
1102
                divcnt <= divcnt - 2'd1;
1103
        else
1104
                next_state(DIV3);
1105
DIV3:
1106
        begin
1107
                res[`LOBYTE] <= divres24[11:0];
1108
                res[`HIBYTE] <= divrem12;
1109
                vf <= divres24[23:12] != {12{divres24[11]}};
1110
                next_state(IFETCH);
1111
        end
1112
 
1113
// ============================================================================
1114
// ============================================================================
1115 2 robfinch
PUSH1:
1116
        begin
1117
                next_state(PUSH2);
1118
                if (isINT | isPSHS) begin
1119
                        wadr <= (ssp - cnt);
1120
                        ssp <= (ssp - cnt);
1121
                end
1122
                else begin      // PSHU
1123 15 robfinch
                        wadr <= ({usppg,{bitsPerByte{1'b0}}} + usp - cnt);
1124 2 robfinch
                        usp <= (usp - cnt);
1125
                end
1126
        end
1127
PUSH2:
1128
        begin
1129
                next_state(STORE1);
1130
                if (ir[bitsPerByte]) begin
1131
                        store_what <= `SW_CCR;
1132
                        ir[bitsPerByte] <= 1'b0;
1133
                end
1134
                else if (ir[bitsPerByte+1]) begin
1135
                        store_what <= `SW_ACCA;
1136
                        ir[bitsPerByte+1] <= 1'b0;
1137
                end
1138
                else if (ir[bitsPerByte+2]) begin
1139
                        store_what <= `SW_ACCB;
1140
                        ir[bitsPerByte+2] <= 1'b0;
1141
                end
1142 21 robfinch
`ifdef SUPPORT_6309
1143
                else if (ir[bitsPerByte+8]) begin
1144
                        store_what <= `SW_ACCE;
1145
                        ir[bitsPerByte+8] <= 1'b0;
1146
                end
1147
                else if (ir[bitsPerByte+9]) begin
1148
                        store_what <= `SW_ACCF;
1149
                        ir[bitsPerByte+9] <= 1'b0;
1150
                end
1151
`endif
1152 2 robfinch
                else if (ir[bitsPerByte+3]) begin
1153
                        store_what <= `SW_DPR;
1154
                        ir[bitsPerByte+3] <= 1'b0;
1155
                end
1156
                else if (ir[bitsPerByte+4]) begin
1157
                        store_what <= `SW_XH;
1158
                        ir[bitsPerByte+4] <= 1'b0;
1159
                end
1160
                else if (ir[bitsPerByte+5]) begin
1161
                        store_what <= `SW_YH;
1162
                        ir[bitsPerByte+5] <= 1'b0;
1163
                end
1164
                else if (ir[bitsPerByte+6]) begin
1165
                        if (isINT | isPSHS)
1166
                                store_what <= `SW_USPH;
1167
                        else
1168
                                store_what <= `SW_SSPH;
1169
                        ir[bitsPerByte+6] <= 1'b0;
1170
                end
1171
                else if (ir[bitsPerByte+7]) begin
1172
                        store_what <= isFar ? `SW_PC2316 : `SW_PCH;
1173
                        ir[bitsPerByte+7] <= 1'b0;
1174
                end
1175
                else begin
1176
                        if (isINT) begin
1177 22 robfinch
                                dm <= 1'b0;
1178 2 robfinch
                                radr <= vect;
1179
                                if (vec_i != 24'h0) begin
1180
                                        $display("vector: %h", vec_i);
1181
                                        pc <= vec_i;
1182
                                        next_state(IFETCH);
1183
                                end
1184
                                else begin
1185
                                        pc[`BYTE3] <= 8'h00;
1186
                                        load_what <= `LW_PCH;
1187
                                        next_state(LOAD1);
1188
                                end
1189
                        end
1190
                        else
1191
                                next_state(IFETCH);
1192
                end
1193
        end
1194
PULL1:
1195
        begin
1196
                next_state(LOAD1);
1197
                if (ir[bitsPerByte]) begin
1198
                        load_what <= `LW_CCR;
1199
                        ir[bitsPerByte] <= 1'b0;
1200
                end
1201
                else if (ir[bitsPerByte+1]) begin
1202
                        load_what <= `LW_ACCA;
1203
                        ir[bitsPerByte+1] <= 1'b0;
1204
                end
1205
                else if (ir[bitsPerByte+2]) begin
1206
                        load_what <= `LW_ACCB;
1207
                        ir[bitsPerByte+2] <= 1'b0;
1208
                end
1209 21 robfinch
`ifdef SUPPORT_6309
1210
                else if (ir[bitsPerByte+8]) begin
1211
                        load_what <= `LW_ACCE;
1212
                        ir[bitsPerByte+8] <= 1'b0;
1213
                end
1214
                else if (ir[bitsPerByte+9]) begin
1215
                        load_what <= `LW_ACCF;
1216
                        ir[bitsPerByte+9] <= 1'b0;
1217
                end
1218
`endif
1219 2 robfinch
                else if (ir[bitsPerByte+3]) begin
1220
                        load_what <= `LW_DPR;
1221
                        ir[bitsPerByte+3] <= 1'b0;
1222
                end
1223
                else if (ir[bitsPerByte+4]) begin
1224
                        load_what <= `LW_XH;
1225
                        ir[bitsPerByte+4] <= 1'b0;
1226
                end
1227
                else if (ir[bitsPerByte+5]) begin
1228
                        load_what <= `LW_YH;
1229
                        ir[bitsPerByte+5] <= 1'b0;
1230
                end
1231
                else if (ir[bitsPerByte+6]) begin
1232
                        if (ir12==`PULU)
1233
                                load_what <= `LW_SSPH;
1234
                        else
1235
                                load_what <= `LW_USPH;
1236
                        ir[bitsPerByte+6] <= 1'b0;
1237
                end
1238
                else if (ir[bitsPerByte+7]) begin
1239
                        load_what <= isFar ? `LW_PC2316 : `LW_PCH;
1240
                        ir[bitsPerByte+7] <= 1'b0;
1241
                end
1242
                else
1243
                        next_state(IFETCH);
1244
        end
1245
 
1246
// ----------------------------------------------------------------------------
1247
// Outer Indexing Support
1248
// ----------------------------------------------------------------------------
1249
OUTER_INDEXING:
1250
        begin
1251
                if (bitsPerByte==8) begin
1252 12 robfinch
                        casez(ndxbyte)
1253
                        8'b0???????:    radr <= radr + ndxreg;
1254
                        8'b1???0000:
1255 2 robfinch
                                                        begin
1256
                                                                radr <= radr + ndxreg;
1257
                                                                case(ndxbyte[6:5])
1258
                                                                2'b00:  xr <= (xr + 2'd1);
1259
                                                                2'b01:  yr <= (yr + 2'd1);
1260
                                                                2'b10:  usp <= (usp + 2'd1);
1261
                                                                2'b11:  ssp <= (ssp + 2'd1);
1262
                                                                endcase
1263
                                                        end
1264 12 robfinch
                        8'b1???0001:    begin
1265 2 robfinch
                                                                radr <= radr + ndxreg;
1266
                                                                case(ndxbyte[6:5])
1267
                                                                2'b00:  xr <= (xr + 2'd2);
1268
                                                                2'b01:  yr <= (yr + 2'd2);
1269
                                                                2'b10:  usp <= (usp + 2'd2);
1270
                                                                2'b11:  ssp <= (ssp + 2'd2);
1271
                                                                endcase
1272
                                                        end
1273 12 robfinch
                        8'b1???0010:    radr <= radr + ndxreg;
1274
                        8'b1???0011:    radr <= radr + ndxreg;
1275
                        8'b1???0100:    radr <= radr + ndxreg;
1276
                        8'b1???0101:    radr <= radr + ndxreg;
1277
                        8'b1???0110:    radr <= radr + ndxreg;
1278
                        8'b1???1000:    radr <= radr + ndxreg;
1279
                        8'b1???1001:    radr <= radr + ndxreg;
1280
                        8'b1???1010:    radr <= radr + ndxreg;
1281
                        8'b1???1011:    radr <= radr + ndxreg;
1282 2 robfinch
                        default:        radr <= radr;
1283
                        endcase
1284
                end
1285
                else if (bitsPerByte==12) begin
1286 12 robfinch
                        casez(ndxbyte)
1287
                        12'b0???????????:       radr <= radr + ndxreg;
1288
                        12'b1????0000000:
1289 2 robfinch
                                                        begin
1290
                                                                radr <= radr + ndxreg;
1291
                                                                case(ndxbyte[10:9])
1292
                                                                2'b00:  xr <= (xr + 2'd1);
1293
                                                                2'b01:  yr <= (yr + 2'd1);
1294
                                                                2'b10:  usp <= (usp + 2'd1);
1295
                                                                2'b11:  ssp <= (ssp + 2'd1);
1296
                                                                endcase
1297
                                                        end
1298 12 robfinch
                        12'b1????0000001:       begin
1299 2 robfinch
                                                                radr <= radr + ndxreg;
1300
                                                                case(ndxbyte[10:9])
1301
                                                                2'b00:  xr <= (xr + 2'd2);
1302
                                                                2'b01:  yr <= (yr + 2'd2);
1303
                                                                2'b10:  usp <= (usp + 2'd2);
1304
                                                                2'b11:  ssp <= (ssp + 2'd2);
1305
                                                                endcase
1306
                                                        end
1307 12 robfinch
                        12'b1????0000010:       radr <= radr + ndxreg;
1308
                        12'b1????0000011:       radr <= radr + ndxreg;
1309
                        12'b1????0000100:       radr <= radr + ndxreg;
1310 22 robfinch
                        12'b1????00?0101:       radr <= radr + ndxreg;
1311
                        12'b1????00?0110:       radr <= radr + ndxreg;
1312 12 robfinch
                        12'b1????0001000:       radr <= radr + ndxreg;
1313
                        12'b1????0001001:       radr <= radr + ndxreg;
1314
                        12'b1????0001010:       radr <= radr + ndxreg;
1315 22 robfinch
                        12'b1????00?1011:       radr <= radr + ndxreg;
1316 2 robfinch
                        default:        radr <= radr;
1317
                        endcase
1318
                end
1319
                next_state(OUTER_INDEXING2);
1320
        end
1321
OUTER_INDEXING2:
1322
        begin
1323
                wadr <= radr;
1324
                res <= radr[`DBLBYTE];
1325
                load_what <= load_what2;
1326
                if (isLEA)
1327
                        next_state(IFETCH);
1328
                else if (isStore)
1329
                        next_state(STORE1);
1330
                else
1331
                        next_state(LOAD1);
1332
        end
1333
 
1334
// ============================================================================
1335
// Cache Control
1336
// ============================================================================
1337
ICACHE1:
1338
        begin
1339
                iccnt <= 4'h0;
1340
                outstanding <= 16'h0;
1341
                if (hit0 & hit1)
1342
                        next_state(IFETCH);
1343
                else if (!tsc && !ack_i) begin
1344
                        rhit0 <= hit0;
1345
                        bte_o <= 2'b00;
1346
                        cti_o <= 3'b001;
1347
                        cyc_o <= 1'b1;
1348
                        bl_o <= 6'd15;
1349
                        stb_o <= 1'b1;
1350
                        we_o <= 1'b0;
1351
                        adr_o <= !hit0 ? {pc[bitsPerByte*3-1:4],4'b00} : {pcp16[bitsPerByte*3-1:4],4'b0000};
1352
                        dat_o <= 12'd0;
1353
                        next_state(ICACHE2);
1354
                end
1355
        end
1356
// If tsc is asserted during an instruction cache fetch, then abort the fetch
1357
// cycle, and wait until tsc deactivates.
1358
// The instruction cache uses asynchronous reading through the network for
1359
// better performance. The read request and the read response are two
1360
// separate things.
1361
ICACHE2:
1362
`ifdef SUPPORT_AREAD
1363
        if (tsc) begin
1364
                wb_nack();
1365
                next_state(ICACHE3);
1366
        end
1367
        else if (ack_i|rty_i|bto) begin
1368
                stb_o <= 1'b0;
1369
                iccnt <= iccnt + 2'd1;
1370
                next_state(ICACHE4);
1371
                if (iccnt==4'b1110)
1372
                        cti_o <= 3'b111;
1373
                if (iccnt==4'b1111) begin
1374
                        icwa <= adr_o;
1375
                        wb_nack();
1376
                        next_state(ICACHE5);
1377
                end
1378
        end
1379
`else
1380
        if (tsc|rty_i) begin
1381
                wb_nack();
1382
                next_state(ICACHE3);
1383
        end
1384
        else if (ack_i) begin
1385
                stb_o <= 1'b0;
1386
                iccnt <= iccnt + 2'd1;
1387
                next_state(ICACHE4);
1388
                if (iccnt==4'b1110)
1389
                        cti_o <= 3'b111;
1390
                if (iccnt==4'b1111) begin
1391
                        icwa <= adr_o;
1392
                        wb_nack();
1393
                        next_state(ICACHE6);
1394
                end
1395
        end
1396
`endif
1397
 
1398
ICACHE4:
1399
        if (!ack_i) begin
1400
                adr_o[3:0] <= iccnt;
1401
                stb_o <= 1'b1;
1402
                next_state(ICACHE2);
1403
        end
1404
 
1405
ICACHE6:
1406
        next_state(ICACHE1);
1407
 
1408
// The following states to handle outstanding transfers.
1409
// The transfer might retry several times if it has not registered.
1410
`ifdef SUPPORT_AREAD
1411
ICACHE5:
1412
        // Line loaded?
1413
        if (icgot == 16'hFFFF)
1414
                next_state(ICACHE6);
1415
        else begin
1416
                waitcnt <= 6'd20;
1417
                next_state(ICACHE7);
1418
        end
1419
ICACHE7:
1420
        if (waitcnt==6'd0) begin
1421 11 robfinch
                next_state(ICACHE5);
1422 2 robfinch
                adr_o <= icwa;
1423
                for (n4 = 15; n4 >= 0; n4 = n4 - 1)
1424 11 robfinch
                        if (~icgot[n4]) begin// & ~outstanding[n4]) begin
1425 2 robfinch
                                cti_o <= 3'b001;
1426 11 robfinch
                                cyc_o <= `TRUE;
1427
                                stb_o <= `TRUE;
1428 2 robfinch
                                adr_o[3:0] <= n4[3:0];
1429
                                outstanding[n4[3:0]] <= 1'b1;
1430
                                next_state(ICACHE9);
1431
                        end
1432
        end
1433
        else
1434
                waitcnt <= waitcnt - 2'd1;
1435
ICACHE9:
1436
        begin
1437
                if (bto)
1438
                        outstanding <= 16'h0;
1439
                if (aack_i)
1440 11 robfinch
                        outstanding[atag_i] <= 1'b0;
1441 2 robfinch
                if (ack_i|rty_i|bto) begin
1442
                        wb_nack();
1443
                        waitcnt <= 6'd20;
1444
                        next_state(ICACHE7);
1445
                end
1446
        end
1447
`endif
1448
 
1449
// Restart a cache load aborted by the TSC signal. A registered version of the
1450
// hit signal must be used as the cache may be partially updated.
1451
ICACHE3:
1452
        if (!tsc) begin
1453
                bte_o <= 2'b00;
1454
                cti_o <= 3'b001;
1455
                cyc_o <= 1'b1;
1456
                bl_o <= 6'd15;
1457
                stb_o <= 1'b1;
1458
                we_o <= 1'b0;
1459
                adr_o <= !rhit0 ? {pc[bitsPerByte*3-1:4],4'b00} : {pcp16[bitsPerByte*3-1:4],4'b0000};
1460
                dat_o <= 12'd0;
1461
                next_state(ICACHE2);
1462
        end
1463
 
1464
`ifdef SUPPORT_IBUF
1465
IBUF1:
1466
        if (!tsc) begin
1467
                bte_o <= 2'b00;
1468
                cti_o <= 3'b001;
1469
                cyc_o <= 1'b1;
1470
                bl_o <= 6'd2;
1471
                stb_o <= 1'b1;
1472
                we_o <= 1'b0;
1473
                adr_o <= pc[`DBLBYTE];
1474
                dat_o <= 12'd0;
1475
                next_state(IBUF2);
1476
        end
1477
IBUF2:
1478
        if (tsc|rty_i) begin
1479
                wb_nack();
1480
                next_state(IBUF1);
1481
        end
1482
        else if (ack_i) begin
1483
                adr_o <= adr_o + 2'd1;
1484
                ibuf <= dat_i;
1485
                next_state(IBUF3);
1486
        end
1487
IBUF3:
1488
        if (tsc|rty_i) begin
1489
                wb_nack();
1490
                next_state(IBUF1);
1491
        end
1492
        else if (ack_i) begin
1493
                cti_o <= 3'b111;
1494
                adr_o <= adr_o + 2'd1;
1495
                ibuf[`HIBYTE] <= dat_i;
1496
                next_state(IBUF4);
1497
        end
1498
IBUF4:
1499
        if (tsc|rty_i) begin
1500
                wb_nack();
1501
                next_state(IBUF1);
1502
        end
1503
        else if (ack_i) begin
1504
                wb_nack();
1505
                ibuf[`BYTE3] <= dat_i;
1506
                next_state(IBUF5);
1507
        end
1508
IBUF5:
1509
        if (tsc|rty_i) begin
1510
                wb_nack();
1511
                next_state(IBUF1);
1512
        end
1513
        else if (ack_i) begin
1514
                wb_nack();
1515
                ibuf[`BYTE4] <= dat_i;
1516
                next_state(IBUF6);
1517
        end
1518
IBUF6:
1519
        if (tsc|rty_i) begin
1520
                wb_nack();
1521
                next_state(IBUF1);
1522
        end
1523
        else if (ack_i) begin
1524
                wb_nack();
1525
                ibuf[`BYTE5] <= dat_i;
1526
                ibufadr <= pc;
1527
                next_state(IFETCH);
1528
        end
1529
`endif
1530
 
1531
endcase
1532
end
1533
 
1534
// ============================================================================
1535
// ============================================================================
1536
// Supporting Tasks
1537
// ============================================================================
1538
// ============================================================================
1539
 
1540
// ============================================================================
1541
// IFETCH
1542
//
1543
// Fetch instructions.
1544
// ============================================================================
1545
 
1546
task tIfetch;
1547
begin
1548
        if (halt_i) begin
1549
                ba_o <= 1'b1;
1550
                bs_o <= 1'b1;
1551
        end
1552
        else begin
1553
                ba_o <= 1'b0;
1554
                bs_o <= 1'b0;
1555
                next_state(DECODE);
1556
                isFar <= `FALSE;
1557 13 robfinch
`ifdef EIGHTBIT
1558 2 robfinch
                isOuterIndexed <= `FALSE;
1559 13 robfinch
`endif
1560 2 robfinch
                ipg <= 2'b00;
1561 12 robfinch
                ia <= {bitsPerByte*3{1'b0}};
1562 2 robfinch
                res <= 24'd0;
1563
                load_what <= `LW_NOTHING;
1564
                store_what <= `SW_NOTHING;
1565
                if (nmi_edge | firq_i | irq_i)
1566
                        sync_state <= `FALSE;
1567
                if (nmi_edge & nmi_armed) begin
1568
                        bs_o <= 1'b1;
1569
                        ir[`LOBYTE] <= `INT;
1570
                        ipg <= 2'b11;
1571
                        vect <= `NMI_VECT;
1572
                end
1573 22 robfinch
                else if ({nmi_i,firq_i,irq_i} > {im1,firqim,im} && !sync_state && iplMd) begin
1574
                        bs_o <= 1'b1;
1575
                        ir[`LOBYTE] <= `INT;
1576
                        ipg <= 2'b11;
1577
                        case({nmi_i,firq_i,irq_i})
1578
                        3'd1:   vect <= `IRQ_VECT;
1579
                        3'd2:   vect <= `FIRQ_VECT;
1580
                        default:        vect <= `DBG_VECT | {nmi_i,firq_i,irq_i,1'b0};
1581
                        endcase
1582
                end
1583 2 robfinch
                else if (firq_i & !firqim & !sync_state) begin
1584
                        bs_o <= 1'b1;
1585
                        ir[`LOBYTE] <= `INT;
1586
                        ipg <= 2'b11;
1587
                        vect <= `FIRQ_VECT;
1588
                end
1589
                else if (irq_i & !im & !sync_state) begin
1590
                        $display("**************************************");
1591
                        $display("****** Interrupt *********************");
1592
                        $display("**************************************");
1593
                        bs_o <= 1'b1;
1594
                        ir[`LOBYTE] <= `INT;
1595
                        ipg <= 2'b11;
1596
                        vect <= `IRQ_VECT;
1597
                end
1598 22 robfinch
`ifdef SUPPORT_DEBUG_REG
1599
                // Check for instruction breakpoint hit.
1600
                else if (brkctrl[0].en && brkctrl[0].match_type==BMT_IA && (pc & {{20{1'b1}},brkctrl[0].amask})==brkad[0]) begin
1601
                        brkctrl[0].hit <= 1'b1;
1602
                        bs_o <= 1'b1;
1603
                        ir[`LOBYTE] <= `INT;
1604
                        ipg <= 2'b11;
1605
                        vect <= `DBG_VECT;
1606
                end
1607
                else if (brkctrl[1].en && brkctrl[1].match_type==BMT_IA && (pc & {{20{1'b1}},brkctrl[1].amask})==brkad[1]) begin
1608
                        brkctrl[1].hit <= 1'b1;
1609
                        bs_o <= 1'b1;
1610
                        ir[`LOBYTE] <= `INT;
1611
                        ipg <= 2'b11;
1612
                        vect <= `DBG_VECT;
1613
                end
1614
                else if (brkctrl[2].en && brkctrl[2].match_type==BMT_IA && (pc & {{20{1'b1}},brkctrl[2].amask})==brkad[2]) begin
1615
                        brkctrl[2].hit <= 1'b1;
1616
                        bs_o <= 1'b1;
1617
                        ir[`LOBYTE] <= `INT;
1618
                        ipg <= 2'b11;
1619
                        vect <= `DBG_VECT;
1620
                end
1621
                else if (brkctrl[3].en && brkctrl[3].match_type==BMT_IA && (pc & {{20{1'b1}},brkctrl[3].amask})==brkad[3]) begin
1622
                        brkctrl[3].hit <= 1'b1;
1623
                        bs_o <= 1'b1;
1624
                        ir[`LOBYTE] <= `INT;
1625
                        ipg <= 2'b11;
1626
                        vect <= `DBG_VECT;
1627
                end
1628
`endif
1629 2 robfinch
                else begin
1630
                        if (sync_state) begin
1631
                                ba_o <= 1'b1;
1632
                                next_state(IFETCH);
1633
                        end
1634
                        else if (icacheOn) begin
1635
                                if (ihit) begin
1636
                                        ir <= insn;
1637
                                end
1638
                                else begin
1639
                                        ipg <= ipg;
1640
                                        isFar <= isFar;
1641 13 robfinch
`ifdef EIGHTBIT
1642 2 robfinch
                                        isOuterIndexed <= isOuterIndexed;
1643 13 robfinch
`endif
1644 2 robfinch
                                        next_state(ICACHE1);
1645
                                end
1646
                        end
1647
`ifdef SUPPORT_IBUF
1648
                        else begin
1649
                                if (ibufhit)
1650
                                        ir <= ibuf;
1651
                                else begin
1652
                                        ipg <= ipg;
1653
                                        isFar <= isFar;
1654 13 robfinch
`ifdef EIGHTBIT
1655 2 robfinch
                                        isOuterIndexed <= isOuterIndexed;
1656 13 robfinch
`endif
1657 2 robfinch
                                        next_state(IBUF1);
1658
                                end
1659
                        end
1660
`endif
1661
                end
1662
        end
1663
end
1664
endtask
1665
 
1666
// ============================================================================
1667
// DECODE
1668
//
1669
// Decode instruction and fetch register file values.
1670
// ============================================================================
1671
 
1672
task tDecode;
1673
begin
1674
        first_ifetch <= `TRUE;
1675
        next_state(IFETCH);             // default: move to IFETCH
1676
        pc <= pc + 2'd1;                // default: increment PC by one
1677
        a <= 24'd0;
1678
        b <= 24'd0;
1679 12 robfinch
        ia <= {bitsPerByte * 3{1'b0}};
1680 2 robfinch
        isDblIndirect <= `FALSE;//ndxbyte[11:4]==8'h8F;
1681
        if (isIndexed) begin
1682
                if (bitsPerByte==8) begin
1683
                        casez(ndxbyte)
1684
                        8'b1??00000:
1685
                                if (!isOuterIndexed)
1686
                                        case(ndxbyte[6:5])
1687
                                        2'b00:  xr <= (xr + 4'd1);
1688
                                        2'b01:  yr <= (yr + 4'd1);
1689
                                        2'b10:  usp <= (usp + 4'd1);
1690
                                        2'b11:  ssp <= (ssp + 4'd1);
1691
                                        endcase
1692
                        8'b1??00001:
1693
                                if (!isOuterIndexed)
1694
                                        case(ndxbyte[6:5])
1695
                                        2'b00:  xr <= (xr + 4'd2);
1696
                                        2'b01:  yr <= (yr + 4'd2);
1697
                                        2'b10:  usp <= (usp + 4'd2);
1698
                                        2'b11:  ssp <= (ssp + 4'd2);
1699
                                        endcase
1700
                        8'b1??00010:
1701
                                case(ndxbyte[6:5])
1702
                                2'b00:  xr <= (xr - 2'd1);
1703
                                2'b01:  yr <= (yr - 2'd1);
1704
                                2'b10:  usp <= (usp - 2'd1);
1705
                                2'b11:  ssp <= (ssp - 2'd1);
1706
                                endcase
1707
                        8'b1??00011:
1708
                                case(ndxbyte[6:5])
1709
                                2'b00:  xr <= (xr - 2'd2);
1710
                                2'b01:  yr <= (yr - 2'd2);
1711
                                2'b10:  usp <= (usp - 2'd2);
1712
                                2'b11:  ssp <= (ssp - 2'd2);
1713
                                endcase
1714
                        endcase
1715
                end
1716
                else if (bitsPerByte==12) begin
1717
                        casez(ndxbyte)
1718
                        12'b1??000000000:
1719 13 robfinch
                                if (!isOuterIndexed && ndxbyte[bitsPerByte-5]==1'b0)
1720 2 robfinch
                                        case(ndxbyte[10:9])
1721
                                        2'b00:  xr <= (xr + 4'd1);
1722
                                        2'b01:  yr <= (yr + 4'd1);
1723
                                        2'b10:  usp <= (usp + 4'd1);
1724
                                        2'b11:  ssp <= (ssp + 4'd1);
1725
                                        endcase
1726
                        12'b1??000000001:
1727 13 robfinch
                                if (!isOuterIndexed && ndxbyte[bitsPerByte-5]==1'b0)
1728 2 robfinch
                                        case(ndxbyte[10:9])
1729
                                        2'b00:  xr <= (xr + 4'd2);
1730
                                        2'b01:  yr <= (yr + 4'd2);
1731
                                        2'b10:  usp <= (usp + 4'd2);
1732
                                        2'b11:  ssp <= (ssp + 4'd2);
1733
                                        endcase
1734 15 robfinch
                        12'b1??0?0000010:
1735 2 robfinch
                                case(ndxbyte[10:9])
1736
                                2'b00:  xr <= (xr - 2'd1);
1737
                                2'b01:  yr <= (yr - 2'd1);
1738
                                2'b10:  usp <= (usp - 2'd1);
1739
                                2'b11:  ssp <= (ssp - 2'd1);
1740
                                endcase
1741 15 robfinch
                        12'b1??0?0000011:
1742 2 robfinch
                                case(ndxbyte[10:9])
1743
                                2'b00:  xr <= (xr - 2'd2);
1744
                                2'b01:  yr <= (yr - 2'd2);
1745
                                2'b10:  usp <= (usp - 2'd2);
1746
                                2'b11:  ssp <= (ssp - 2'd2);
1747
                                endcase
1748
                        endcase
1749
                end
1750
        end
1751
        case(ir12)
1752
        `NOP:   ;
1753
        `SYNC:  sync_state <= `TRUE;
1754
        `ORCC:  begin
1755
                        cf <= cf | ir[bitsPerByte];
1756
                        vf <= vf | ir[bitsPerByte+1];
1757
                        zf <= zf | ir[bitsPerByte+2];
1758
                        nf <= nf | ir[bitsPerByte+3];
1759
                        im <= im | ir[bitsPerByte+4];
1760
                        hf <= hf | ir[bitsPerByte+5];
1761
                        firqim <= firqim | ir[bitsPerByte+6];
1762
                        ef <= ef | ir[bitsPerByte+7];
1763 22 robfinch
                        dm <= dm | ir[bitsPerByte+8];
1764 2 robfinch
                        pc <= pcp2;
1765
                        end
1766
        `ANDCC:
1767
                        begin
1768
                        cf <= cf & ir[bitsPerByte];
1769
                        vf <= vf & ir[bitsPerByte+1];
1770
                        zf <= zf & ir[bitsPerByte+2];
1771
                        nf <= nf & ir[bitsPerByte+3];
1772
                        im <= im & ir[bitsPerByte+4];
1773
                        hf <= hf & ir[bitsPerByte+5];
1774
                        firqim <= firqim & ir[bitsPerByte+6];
1775
                        ef <= ef & ir[bitsPerByte+7];
1776 22 robfinch
                        dm <= dm & ir[bitsPerByte+8];
1777 2 robfinch
                        pc <= pcp2;
1778
                        end
1779
        `DAA:
1780
                        begin
1781
                                if (hf || acca[3:0] > 4'd9)
1782
                                        res12[3:0] <= acca[3:0] + 4'd6;
1783
                                if (cf || acca[7:4] > 4'd9 || (acca[7:4] > 4'd8 && acca[3:0] > 4'd9))
1784
                                        res12[8:4] <= acca[7:4] + 4'd6;
1785
                        end
1786
        `CWAI:
1787
                        begin
1788
                        cf <= cf & ir[bitsPerByte];
1789
                        vf <= vf & ir[bitsPerByte+1];
1790
                        zf <= zf & ir[bitsPerByte+2];
1791
                        nf <= nf & ir[bitsPerByte+3];
1792
                        im <= im & ir[bitsPerByte+4];
1793
                        hf <= hf & ir[bitsPerByte+5];
1794
                        firqim <= firqim & ir[bitsPerByte+6];
1795 22 robfinch
                        dm <= dm & ir[bitsPerByte+8];
1796 2 robfinch
                        ef <= 1'b1;
1797
                        pc <= pc + 2'd2;
1798
                        ir[`HIBYTE] <= -1;
1799
                        isFar <= `TRUE;
1800
                        wait_state <= `TRUE;
1801
                        next_state(PUSH1);
1802
                        end
1803
        `LDMD:  begin
1804
                        natMd <= ir[bitsPerByte];
1805
                        firqMd <= ir[bitsPerByte+1];
1806 22 robfinch
                        iplMd <= ir[bitsPerByte+2];
1807 2 robfinch
                        pc <= pc + 2'd2;
1808
                        end
1809 22 robfinch
        `BITMD:
1810
                begin
1811
                        res <= {dbz,iop,3'd0,iplMd,firqMd,natMd} & ir[`BYTE2];
1812
                        if (ir[bitsPerByte+7])
1813
                                dbz <= 1'b0;
1814
                        if (ir[bitsPerByte+6])
1815
                                iop <= 1'b0;
1816
                        pc <= pc + 2'd2;
1817
                end
1818 2 robfinch
        `TFR:   pc <= pc + 2'd2;
1819
        `EXG:   pc <= pc + 2'd2;
1820
        `ABX:   res <= xr + accb;
1821
        `SEX: res <= {{bitsPerByte{accb[BPBM1]}},accb[`LOBYTE]};
1822
        `PG2:   begin ipg <= 2'b01; ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1823
        `PG3:   begin ipg <= 2'b10; ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1824
        `FAR:   begin isFar <= `TRUE;  ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1825 13 robfinch
`ifdef EIGHTBIT
1826 2 robfinch
        `OUTER: begin isOuterIndexed <= `TRUE;  ir <= ir[bitsPerByte*5-1:bitsPerByte]; next_state(DECODE); end
1827 13 robfinch
`endif
1828 22 robfinch
        `NEGA,`NEGB:
1829
                if (dm) begin
1830
                        a <= 'b0; b <= acc;
1831
                        next_state(CALC);
1832
                end
1833
                else begin res12 <= -acc[`LOBYTE]; a <= 24'h00; b <= acc; end
1834 2 robfinch
        `COMA,`COMB:    begin res12 <= ~acc[`LOBYTE]; end
1835
        `LSRA,`LSRB:    begin res12 <= {acc[0],1'b0,acc[BPBM1:1]}; end
1836
        `RORA,`RORB:    begin res12 <= {acc[0],cf,acc[BPBM1:1]}; end
1837
        `ASRA,`ASRB:    begin res12 <= {acc[0],acc[BPBM1],acc[BPBM1:1]}; end
1838
        `ASLA,`ASLB:    begin res12 <= {acc[`LOBYTE],1'b0}; end
1839
        `ROLA,`ROLB:    begin res12 <= {acc[`LOBYTE],cf}; end
1840
        `DECA,`DECB:    begin res12 <= acc[`LOBYTE] - 2'd1; end
1841
        `INCA,`INCB:    begin res12 <= acc[`LOBYTE] + 2'd1; end
1842
        `TSTA,`TSTB:    begin res12 <= acc[`LOBYTE]; end
1843
        `CLRA,`CLRB:    begin res12 <= 13'h000; end
1844 22 robfinch
        `MUL:   if (dm) begin divcnt <= 6'd17; next_state(MUL2); end
1845 21 robfinch
`ifdef SUPPORT_6309
1846
        `TSTD:  res <= {acca,accb};
1847
        `TSTW:  res <= {acce,accf};
1848
        `TSTE:  res12 <= acce;
1849
        `TSTF:  res12 <= accf;
1850 22 robfinch
        `NEGD:
1851
                if (dm) begin
1852
                        a <= 'd0; b <= {acca,accb};
1853
                        next_state(CALC);
1854
                end
1855
                else begin res <= -{acca,accb}; a <= 'd0; b <= {acca,accb}; end
1856 21 robfinch
        `INCE,`INCF:    begin res12 <= acc[`LOBYTE] + 2'd1; end
1857
        `INCD:  res <= {acca,accb} + 2'd1;
1858
        `INCW:  res <= {acce,accf} + 2'd1;
1859
        `DECE,`DECF:    begin res12 <= acc[`LOBYTE] - 2'd1; end
1860
        `DECD:  res <= {acca,accb} - 2'd1;
1861
        `DECW:  res <= {acce,accf} - 2'd1;
1862
        `COMD:  res <= ~{acca,accb};
1863
        `COME,`COMF:    res <= ~acc[`LOBYTE];
1864
        `COMW:  res <= ~{acce,accf};
1865
        `CLRD:  res <= 'b0;
1866
        `CLRW:  res <= 'b0;
1867
        `CLRE:  res12 <= 'b0;
1868
        `CLRF:  res12 <= 'b0;
1869
        `ASLD:
1870
                res <= {acca,accb,1'b0};
1871
        `ASRD:
1872
                res <= {accb[0],acca[bitsPerByte-1],acca,accb[bitsPerByte-1:1]};
1873
        `LSRD:
1874
                res <= {accb[0],acca,accb[bitsPerByte-1:1]};
1875
        `LSRW:
1876
                res <= {accf[0],accw,accf[bitsPerByte-1:1]};
1877
        `ROLD:
1878
                res <= {acca,accb,cf};
1879
        `ROLW:
1880
                res <= {acce,accf,cf};
1881
        `RORD:
1882
                res <= {accb[0],cf,acca,accb[bitsPerByte-1:1]};
1883
        `RORW:
1884
                res <= {accf[0],cf,acce,accf[bitsPerByte-1:1]};
1885
        `ADDR:
1886
                begin
1887
                        case(ir[bitsPerByte+3:bitsPerByte])
1888
                        4'b0000:        begin {acca,accb} <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1889
                        4'b0001:        begin xr <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1890
                        4'b0010:        begin yr <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1891
                        4'b0011:        begin usp <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1892
                        4'b0100:        begin ssp <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1893
                        4'b0101:        begin pc <= sum12; nf <= sum12[bitsPerByte*2-1]; zf <= sum12[`DBLBYTE]=='b0; cf <= sum12[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12[bitsPerByte*2-1]); end
1894
                        4'b1000:        begin acca <= sum12; nf <= sum12[bitsPerByte-1]; zf <= sum12[`LOBYTE]=='b0; cf <= sum12[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12[bitsPerByte-1]); end
1895
                        4'b1001:        begin accb <= sum12; nf <= sum12[bitsPerByte-1]; zf <= sum12[`LOBYTE]=='b0; cf <= sum12[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12[bitsPerByte-1]); end
1896
                        4'b1010:
1897
                                begin
1898
                                        cf <= sum12[0];
1899
                                        vf <= sum12[1];
1900
                                        zf <= sum12[2];
1901
                                        nf <= sum12[3];
1902
                                        im <= sum12[4];
1903
                                        hf <= sum12[5];
1904
                                        firqim <= sum12[6];
1905
                                        ef <= sum12[7];
1906 22 robfinch
                                        dm <= sum12[8];
1907 21 robfinch
                                end
1908
                        4'b1011:        begin dpr <= sum12; nf <= sum12[bitsPerByte-1]; zf <= sum12[`LOBYTE]=='b0; cf <= sum12[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12[bitsPerByte-1]); end
1909
                        endcase
1910
                end
1911
        `ADCR:
1912
                begin
1913
                        case(ir[bitsPerByte+3:bitsPerByte])
1914
                        4'b0000:        begin {acca,accb} <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1915
                        4'b0001:        begin xr <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1916
                        4'b0010:        begin yr <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1917
                        4'b0011:        begin usp <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1918
                        4'b0100:        begin ssp <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1919
                        4'b0101:        begin pc <= sum12c; nf <= sum12c[bitsPerByte*2-1]; zf <= sum12c[`DBLBYTE]=='b0; cf <= sum12c[bitsPerByte*2]; vf <= fnAddOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],sum12c[bitsPerByte*2-1]); end
1920
                        4'b1000:        begin acca <= sum12c; nf <= sum12c[bitsPerByte-1]; zf <= sum12c[`LOBYTE]=='b0; cf <= sum12c[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12c[bitsPerByte-1]); end
1921
                        4'b1001:        begin accb <= sum12c; nf <= sum12c[bitsPerByte-1]; zf <= sum12c[`LOBYTE]=='b0; cf <= sum12c[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12c[bitsPerByte-1]); end
1922
                        4'b1010:
1923
                                begin
1924
                                        cf <= sum12c[0];
1925
                                        vf <= sum12c[1];
1926
                                        zf <= sum12c[2];
1927
                                        nf <= sum12c[3];
1928
                                        im <= sum12c[4];
1929
                                        hf <= sum12c[5];
1930
                                        firqim <= sum12c[6];
1931
                                        ef <= sum12c[7];
1932 22 robfinch
                                        dm <= sum12c[8];
1933 21 robfinch
                                end
1934
                        4'b1011:        begin dpr <= sum12c; nf <= sum12c[bitsPerByte-1]; zf <= sum12c[`LOBYTE]=='b0; cf <= sum12c[bitsPerByte]; vf <= fnAddOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],sum12c[bitsPerByte-1]); end
1935
                        endcase
1936
                end
1937
        `ANDR:
1938
                begin
1939
                        case(ir[bitsPerByte+3:bitsPerByte])
1940
                        4'b0000:        begin {acca,accb} <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1941
                        4'b0001:        begin xr <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1942
                        4'b0010:        begin yr <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1943
                        4'b0011:        begin usp <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1944
                        4'b0100:        begin ssp <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1945
                        4'b0101:        begin pc <= and12; nf <= and12[bitsPerByte*2-1]; zf <= and12[`DBLBYTE]=='b0; vf <= 1'b0; end
1946
                        4'b1000:        begin acca <= and12; nf <= and12[bitsPerByte-1]; zf <= and12[`LOBYTE]=='b0; vf <= 1'b0; end
1947
                        4'b1001:        begin accb <= and12; nf <= and12[bitsPerByte-1]; zf <= and12[`LOBYTE]=='b0; vf <= 1'b0; end
1948
                        4'b1010:
1949
                                begin
1950
                                        cf <= and12[0];
1951
                                        vf <= and12[1];
1952
                                        zf <= and12[2];
1953
                                        nf <= and12[3];
1954
                                        im <= and12[4];
1955
                                        hf <= and12[5];
1956
                                        firqim <= and12[6];
1957
                                        ef <= and12[7];
1958 22 robfinch
                                        dm <= and12[8];
1959 21 robfinch
                                end
1960
                        4'b1011:        begin dpr <= and12; nf <= and12[bitsPerByte-1]; zf <= and12[`LOBYTE]=='b0; vf <= 1'b0; end
1961
                        endcase
1962
                end
1963
        `EORR:
1964
                begin
1965
                        case(ir[bitsPerByte+3:bitsPerByte])
1966
                        4'b0000:        begin {acca,accb} <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1967
                        4'b0001:        begin xr <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1968
                        4'b0010:        begin yr <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1969
                        4'b0011:        begin usp <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1970
                        4'b0100:        begin ssp <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1971
                        4'b0101:        begin pc <= eor12; nf <= eor12[bitsPerByte*2-1]; zf <= eor12[`DBLBYTE]=='b0; vf <= 1'b0; end
1972
                        4'b1000:        begin acca <= eor12; nf <= eor12[bitsPerByte-1]; zf <= eor12[`LOBYTE]=='b0; vf <= 1'b0; end
1973
                        4'b1001:        begin accb <= eor12; nf <= eor12[bitsPerByte-1]; zf <= eor12[`LOBYTE]=='b0; vf <= 1'b0; end
1974
                        4'b1010:
1975
                                begin
1976
                                        cf <= eor12[0];
1977
                                        vf <= eor12[1];
1978
                                        zf <= eor12[2];
1979
                                        nf <= eor12[3];
1980
                                        im <= eor12[4];
1981
                                        hf <= eor12[5];
1982
                                        firqim <= eor12[6];
1983
                                        ef <= eor12[7];
1984 22 robfinch
                                        dm <= eor12[8];
1985 21 robfinch
                                end
1986
                        4'b1011:        begin dpr <= eor12; nf <= eor12[bitsPerByte-1]; zf <= eor12[`LOBYTE]=='b0; vf <= 1'b0; end
1987
                        endcase
1988
                end
1989
        `ORR:
1990
                begin
1991
                        case(ir[bitsPerByte+3:bitsPerByte])
1992
                        4'b0000:        begin {acca,accb} <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1993
                        4'b0001:        begin xr <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1994
                        4'b0010:        begin yr <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1995
                        4'b0011:        begin usp <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1996
                        4'b0100:        begin ssp <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1997
                        4'b0101:        begin pc <= or12; nf <= or12[bitsPerByte*2-1]; zf <= or12[`DBLBYTE]=='b0; vf <= 1'b0; end
1998
                        4'b1000:        begin acca <= or12; nf <= or12[bitsPerByte-1]; zf <= or12[`LOBYTE]=='b0; vf <= 1'b0; end
1999
                        4'b1001:        begin accb <= or12; nf <= or12[bitsPerByte-1]; zf <= or12[`LOBYTE]=='b0; vf <= 1'b0; end
2000
                        4'b1010:
2001
                                begin
2002
                                        cf <= or12[0];
2003
                                        vf <= or12[1];
2004
                                        zf <= or12[2];
2005
                                        nf <= or12[3];
2006
                                        im <= or12[4];
2007
                                        hf <= or12[5];
2008
                                        firqim <= or12[6];
2009
                                        ef <= or12[7];
2010 22 robfinch
                                        dm <= or12[8];
2011 21 robfinch
                                end
2012
                        4'b1011:        begin dpr <= or12; nf <= or12[bitsPerByte-1]; zf <= or12[`LOBYTE]=='b0; vf <= 1'b0; end
2013
                        endcase
2014
                end
2015
        `CMPR:
2016
                begin
2017
                        case(ir[bitsPerByte+3:bitsPerByte])
2018
                        4'b0000:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2019
                        4'b0001:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2020
                        4'b0010:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2021
                        4'b0011:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2022
                        4'b0100:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2023
                        4'b0101:        begin nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2024
                        4'b1000:        begin nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
2025
                        4'b1001:        begin nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
2026
                        4'b1010:        ;
2027
                        4'b1011:        begin nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
2028
                        endcase
2029
                end
2030
        `SBCR:
2031
                begin
2032
                        case(ir[bitsPerByte+3:bitsPerByte])
2033
                        4'b0000:        begin {acca,accb} <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
2034
                        4'b0001:        begin xr <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
2035
                        4'b0010:        begin yr <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
2036
                        4'b0011:        begin usp <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
2037
                        4'b0100:        begin ssp <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
2038
                        4'b0101:        begin pc <= dif12c; nf <= dif12c[bitsPerByte*2-1]; zf <= dif12c[`DBLBYTE]=='b0; cf <= dif12c[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12c[bitsPerByte*2-1]); end
2039
                        4'b1000:        begin acca <= dif12c; nf <= dif12c[bitsPerByte-1]; zf <= dif12c[`LOBYTE]=='b0; cf <= dif12c[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12c[bitsPerByte-1]); end
2040
                        4'b1001:        begin accb <= dif12c; nf <= dif12c[bitsPerByte-1]; zf <= dif12c[`LOBYTE]=='b0; cf <= dif12c[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12c[bitsPerByte-1]); end
2041
                        4'b1010:
2042
                                begin
2043
                                        cf <= dif12c[0];
2044
                                        vf <= dif12c[1];
2045
                                        zf <= dif12c[2];
2046
                                        nf <= dif12c[3];
2047
                                        im <= dif12c[4];
2048
                                        hf <= dif12c[5];
2049
                                        firqim <= dif12c[6];
2050
                                        ef <= dif12c[7];
2051 22 robfinch
                                        dm <= dif12c[8];
2052 21 robfinch
                                end
2053
                        4'b1011:        begin dpr <= dif12c; nf <= dif12c[bitsPerByte-1]; zf <= dif12c[`LOBYTE]=='b0; cf <= dif12c[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12c[bitsPerByte-1]); end
2054
                        endcase
2055
                end
2056
        `SUBR:
2057
                begin
2058
                        case(ir[bitsPerByte+3:bitsPerByte])
2059
                        4'b0000:        begin {acca,accb} <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2060
                        4'b0001:        begin xr <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2061
                        4'b0010:        begin yr <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2062
                        4'b0011:        begin usp <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2063
                        4'b0100:        begin ssp <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2064
                        4'b0101:        begin pc <= dif12; nf <= dif12[bitsPerByte*2-1]; zf <= dif12[`DBLBYTE]=='b0; cf <= dif12[bitsPerByte*2]; vf <= fnSubOverflow(src1[bitsPerByte*2-1],src2[bitsPerByte*2-1],dif12[bitsPerByte*2-1]); end
2065
                        4'b1000:        begin acca <= dif12; nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
2066
                        4'b1001:        begin accb <= dif12; nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
2067
                        4'b1010:
2068
                                begin
2069
                                        cf <= dif12[0];
2070
                                        vf <= dif12[1];
2071
                                        zf <= dif12[2];
2072
                                        nf <= dif12[3];
2073
                                        im <= dif12[4];
2074
                                        hf <= dif12[5];
2075
                                        firqim <= dif12[6];
2076
                                        ef <= dif12[7];
2077 22 robfinch
                                        dm <= dif12[8];
2078 21 robfinch
                                end
2079
                        4'b1011:        begin dpr <= dif12; nf <= dif12[bitsPerByte-1]; zf <= dif12[`LOBYTE]=='b0; cf <= dif12[bitsPerByte]; vf <= fnSubOverflow(src1[bitsPerByte-1],src2[bitsPerByte-1],dif12[bitsPerByte-1]); end
2080
                        endcase
2081
                end
2082
`endif
2083
`ifdef SUPPORT_6309
2084
        `CMPE_IMM,`CMPF_IMM,`SUBE_IMM,`SUBF_IMM:
2085
                begin res12 <= acc[`LOBYTE] - ir[`HIBYTE]; pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2086
        `LDE_IMM,`LDF_IMM:
2087
                begin res12 <= ir[`HIBYTE]; pc <= pc + 2'd2; end
2088
`endif
2089 2 robfinch
        // Immediate mode instructions
2090
        `SUBA_IMM,`SUBB_IMM,`CMPA_IMM,`CMPB_IMM:
2091 22 robfinch
                if (dm) begin pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; next_state(CALC); end
2092
                else begin res12 <= acc[`LOBYTE] - ir[`HIBYTE]; pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2093 2 robfinch
        `SBCA_IMM,`SBCB_IMM:
2094 22 robfinch
                if (dm) begin pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; next_state(CALC); end
2095
                else begin res12 <= acc[`LOBYTE] - ir[`HIBYTE] - cf; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2096 2 robfinch
        `ANDA_IMM,`ANDB_IMM,`BITA_IMM,`BITB_IMM:
2097
                begin res12 <= acc[`LOBYTE] & ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2098
        `LDA_IMM,`LDB_IMM:
2099
                begin res12 <= ir[`HIBYTE]; pc <= pc + 2'd2; end
2100
        `EORA_IMM,`EORB_IMM:
2101
                begin res12 <= acc[`LOBYTE] ^ ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2102
        `ADCA_IMM,`ADCB_IMM:
2103 22 robfinch
                if (dm) begin pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; next_state(CALC); end
2104
                else begin res12 <= acc[`LOBYTE] + ir[`HIBYTE] + cf; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2105 2 robfinch
        `ORA_IMM,`ORB_IMM:
2106
                begin res12 <= acc[`LOBYTE] | ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2107
        `ADDA_IMM,`ADDB_IMM:
2108 22 robfinch
                if (dm) begin pc <= pc + 4'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; next_state(CALC); end
2109
                else begin res12 <= acc[`LOBYTE] + ir[`HIBYTE]; pc <= pc + 2'd2; a <= acc[`LOBYTE]; b <= ir[`HIBYTE]; end
2110 21 robfinch
`ifdef SUPPORT_6309
2111
        `BITD_IMM,
2112
        `ANDD_IMM:
2113
                        begin
2114
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} & {ir[`BYTE2],ir[`BYTE3]};
2115
                                pc <= pc + 32'd3;
2116
                        end
2117
        `EORD_IMM:
2118
                        begin
2119
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} ^ {ir[`BYTE2],ir[`BYTE3]};
2120
                                pc <= pc + 32'd3;
2121
                        end
2122
        `ORD_IMM:
2123
                        begin
2124
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} | {ir[`BYTE2],ir[`BYTE3]};
2125
                                pc <= pc + 32'd3;
2126
                        end
2127
`endif
2128 2 robfinch
        `ADDD_IMM:
2129 22 robfinch
                        if (dm) begin
2130
                                a <= {acca,accb};
2131
                                b <= {ir[`BYTE2],ir[`BYTE3]};
2132
                                pc <= pc + 2'd3;
2133
                                next_state(CALC);
2134
                        end
2135
                        else begin
2136 21 robfinch
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} + {ir[`HIBYTE],ir[`BYTE3]};
2137
                                pc <= pc + 2'd3;
2138
                        end
2139
`ifdef SUPPORT_6309
2140
        `ADDW_IMM:
2141
                        begin
2142
                                res <= {acce[`LOBYTE],accf[`LOBYTE]} + {ir[`HIBYTE],ir[`BYTE3]};
2143
                                pc <= pc + 2'd3;
2144
                        end
2145
        `ADCD_IMM:
2146 22 robfinch
                        if (dm) begin
2147
                                a <= {acca,accb};
2148
                                b <= {ir[`BYTE2],ir[`BYTE3]};
2149
                                pc <= pc + 32'd3;
2150
                                next_state(CALC);
2151
                        end
2152
                        else begin
2153 21 robfinch
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} + {ir[`BYTE2],ir[`BYTE3]} + {23'b0,cf};
2154
                                pc <= pc + 32'd3;
2155
                        end
2156
`endif
2157
        `SUBD_IMM:
2158 22 robfinch
                        if (dm) begin
2159
                                a <= {acca,accb};
2160
                                b <= {ir[`BYTE2],ir[`BYTE3]};
2161
                                pc <= pc + 32'd3;
2162
                                next_state(CALC);
2163
                        end
2164
                        else begin
2165
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
2166
                                pc <= pc + 2'd3;
2167
                        end
2168 21 robfinch
`ifdef SUPPORT_6309
2169
        `SUBW_IMM:
2170 2 robfinch
                                begin
2171 21 robfinch
                                        res <= {acce[`LOBYTE],accf[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
2172 2 robfinch
                                        pc <= pc + 2'd3;
2173
                                end
2174 21 robfinch
        `SBCD_IMM:
2175 22 robfinch
                if (dm) begin
2176
                        a <= {acca,accb};
2177
                        b <= {ir[`BYTE2],ir[`BYTE3]};
2178
                        pc <= pc + 32'd3;
2179
                        next_state(CALC);
2180
                end
2181
                else begin
2182 21 robfinch
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`BYTE2],ir[`BYTE3]} - {23'b0,cf};
2183
                        pc <= pc + 32'd3;
2184
                end
2185
        `LDW_IMM:
2186
                begin
2187
                        res <= {ir[`HIBYTE],ir[`BYTE3]};
2188
                        pc <= pc + 2'd3;
2189
                end
2190
`endif
2191 2 robfinch
        `LDD_IMM:
2192
                                begin
2193
                                        res <= {ir[`HIBYTE],ir[`BYTE3]};
2194
                                        pc <= pc + 2'd3;
2195
                                end
2196
        `LDX_IMM,`LDY_IMM,`LDU_IMM,`LDS_IMM:
2197
                                begin
2198
                                        res <= {ir[`HIBYTE],ir[`BYTE3]};
2199
                                        pc <= pc + 2'd3;
2200
                                end
2201
 
2202
        `CMPD_IMM:
2203 22 robfinch
                        if (dm) begin
2204
                                a <= {acca,accb};
2205
                                b <= {ir[`BYTE2],ir[`BYTE3]};
2206
                                pc <= pc + 32'd3;
2207
                                next_state(CALC);
2208
                        end
2209
                        else begin
2210
                                res <= {acca[`LOBYTE],accb[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
2211
                                pc <= pc + 2'd3;
2212
                                a <= {acca[`LOBYTE],accb[`LOBYTE]};
2213
                                b <= {ir[`HIBYTE],ir[`BYTE3]};
2214
                        end
2215 21 robfinch
`ifdef SUPPORT_6309
2216
        `CMPW_IMM:
2217
                                begin
2218
                                        res <= {acce[`LOBYTE],accf[`LOBYTE]} - {ir[`HIBYTE],ir[`BYTE3]};
2219
                                        pc <= pc + 2'd3;
2220
                                        a <= {acce[`LOBYTE],accf[`LOBYTE]};
2221
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
2222
                                end
2223
`endif
2224 2 robfinch
        `CMPX_IMM:
2225
                                begin
2226
                                        res <= xr[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
2227
                                        pc <= pc + 2'd3;
2228
                                        a <= xr[`DBLBYTE];
2229
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
2230
                                end
2231
        `CMPY_IMM:
2232
                                begin
2233
                                        res <= yr[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
2234
                                        pc <= pc + 2'd3;
2235
                                        a <= yr[`DBLBYTE];
2236
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
2237
                                end
2238
        `CMPU_IMM:
2239
                                begin
2240
                                        res <= usp[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
2241
                                        pc <= pc + 2'd3;
2242
                                        a <= usp[`DBLBYTE];
2243
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
2244
                                end
2245
        `CMPS_IMM:
2246
                                begin
2247
                                        res <= ssp[`DBLBYTE] - {ir[`HIBYTE],ir[`BYTE3]};
2248
                                        pc <= pc + 2'd3;
2249
                                        a <= ssp[`DBLBYTE];
2250
                                        b <= {ir[`HIBYTE],ir[`BYTE3]};
2251
                                end
2252
 
2253
        // Direct mode instructions
2254
        `NEG_DP,`COM_DP,`LSR_DP,`ROR_DP,`ASR_DP,`ASL_DP,`ROL_DP,`DEC_DP,`INC_DP,`TST_DP:
2255
                begin
2256
                        load_what <= `LW_BL;
2257
                        radr <= dp_address;
2258
                        pc <= pc + 2'd2;
2259
                        next_state(LOAD1);
2260
                end
2261 21 robfinch
`ifdef SUPPORT_6309
2262 22 robfinch
        `DIVD_IMM:
2263
                begin
2264
                        b <= {ir[`BYTE3],ir[`BYTE2]};
2265
                        pc <= pc + 2'd2;
2266
                        next_state(DIV1);
2267
                end
2268
        `DIVQ_IMM:
2269
                begin
2270
                        b <= {ir[`BYTE3],ir[`BYTE2]};
2271
                        pc <= pc + 2'd3;
2272
                        next_state(DIV1);
2273
                end
2274
        `MULD_IMM:
2275
                begin
2276
                        b <= {ir[`BYTE3],ir[`BYTE2]};
2277
                        pc <= pc + 2'd3;
2278
                        divcnt <= 6'd7;
2279
                        next_state(MUL2);
2280
                end
2281
`endif
2282
`ifdef SUPPORT_6309
2283 21 robfinch
        `CMPE_DP,`CMPF_DP,
2284
        `LDE_DP,`LDF_DP,
2285
        `SUBE_DP,`SUBF_DP,
2286
`endif
2287 2 robfinch
        `SUBA_DP,`CMPA_DP,`SBCA_DP,`ANDA_DP,`BITA_DP,`LDA_DP,`EORA_DP,`ADCA_DP,`ORA_DP,`ADDA_DP,
2288
        `SUBB_DP,`CMPB_DP,`SBCB_DP,`ANDB_DP,`BITB_DP,`LDB_DP,`EORB_DP,`ADCB_DP,`ORB_DP,`ADDB_DP:
2289
                begin
2290
                        load_what <= `LW_BL;
2291
                        radr <= dp_address;
2292
                        pc <= pc + 2'd2;
2293
                        next_state(LOAD1);
2294
                end
2295 21 robfinch
`ifdef SUPPORT_6309
2296
        `BITD_DP,
2297
        `ANDD_DP,
2298
        `ORD_DP,
2299 22 robfinch
        `DIVD_DP,
2300
        `DIVQ_DP,
2301 21 robfinch
        `EORD_DP:
2302
                begin
2303
                        load_what <= `LW_BL;
2304
                        radr <= dp_address;
2305
                        pc <= pc + 2'd2;
2306
                        next_state(LOAD1);
2307
                end
2308
`endif
2309
`ifdef SUPPORT_6309
2310 22 robfinch
        `MULD_DP,
2311 21 robfinch
        `ADDW_DP,`CMPW_DP,`LDW_DP,`SUBW_DP,
2312
`endif
2313 2 robfinch
        `SUBD_DP,`ADDD_DP,`LDD_DP,`CMPD_DP,`ADCD_DP,`SBCD_DP:
2314
                begin
2315
                        load_what <= `LW_BH;
2316
                        pc <= pc + 2'd2;
2317
                        radr <= dp_address;
2318
                        next_state(LOAD1);
2319
                end
2320
        `CMPX_DP,`LDX_DP,`LDU_DP,`LDS_DP,
2321
        `CMPY_DP,`CMPS_DP,`CMPU_DP,`LDY_DP:
2322
                begin
2323
                        load_what <= `LW_BH;
2324
                        pc <= pc + 2'd2;
2325
                        radr <= dp_address;
2326
                        next_state(LOAD1);
2327
                end
2328
        `CLR_DP:
2329
                begin
2330
                        dp_store(`SW_RES8);
2331
                        res12 <= 13'h000;
2332
                end
2333
        `STA_DP:        dp_store(`SW_ACCA);
2334
        `STB_DP:        dp_store(`SW_ACCB);
2335
        `STD_DP:        dp_store(`SW_ACCDH);
2336
        `STU_DP:        dp_store(`SW_USPH);
2337
        `STS_DP:        dp_store(`SW_SSPH);
2338
        `STX_DP:        dp_store(`SW_XH);
2339
        `STY_DP:        dp_store(`SW_YH);
2340 21 robfinch
`ifdef SUPPORT_6309
2341
        `STW_DP:        dp_store(`SW_ACCWH);
2342
        `STE_DP:        dp_store(`SW_ACCE);
2343
        `STF_DP:        dp_store(`SW_ACCF);
2344
`endif
2345 2 robfinch
        // Indexed mode instructions
2346
        `NEG_NDX,`COM_NDX,`LSR_NDX,`ROR_NDX,`ASR_NDX,`ASL_NDX,`ROL_NDX,`DEC_NDX,`INC_NDX,`TST_NDX:
2347
                begin
2348
                        pc <= pc + insnsz;
2349
                        if (isIndirect) begin
2350
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2351
                                load_what2 <= `LW_BL;
2352
                                radr <= NdxAddr;
2353
                                next_state(LOAD1);
2354
                        end
2355
                        else begin
2356
                                b <= 24'd0;
2357
                                load_what <= `LW_BL;
2358
                                radr <= NdxAddr;
2359
                                next_state(LOAD1);
2360
                        end
2361
                end
2362 21 robfinch
`ifdef SUPPORT_6309
2363
        `CMPE_NDX,`CMPF_NDX,
2364
        `LDE_NDX,`LDF_NDX,
2365
        `SUBE_NDX,`SUBF_NDX,
2366
`endif
2367 2 robfinch
        `SUBA_NDX,`CMPA_NDX,`SBCA_NDX,`ANDA_NDX,`BITA_NDX,`LDA_NDX,`EORA_NDX,`ADCA_NDX,`ORA_NDX,`ADDA_NDX,
2368
        `SUBB_NDX,`CMPB_NDX,`SBCB_NDX,`ANDB_NDX,`BITB_NDX,`LDB_NDX,`EORB_NDX,`ADCB_NDX,`ORB_NDX,`ADDB_NDX:
2369
                begin
2370
                        pc <= pc + insnsz;
2371
                        if (isIndirect) begin
2372
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2373
                                load_what2 <= `LW_BL;
2374
                                radr <= NdxAddr;
2375
                                next_state(LOAD1);
2376
                        end
2377
                        else begin
2378
                                b <= 24'd0;
2379
                                load_what <= `LW_BL;
2380
                                radr <= NdxAddr;
2381
                                next_state(LOAD1);
2382
                        end
2383
                end
2384 21 robfinch
`ifdef SUPPORT_6309
2385
        `BITD_NDX,
2386
        `ANDD_NDX,
2387
        `ORD_NDX,
2388 22 robfinch
        `DIVD_NDX,
2389
        `DIVQ_NDX,
2390 21 robfinch
        `EORD_NDX:
2391
                begin
2392
                        pc <= pc + insnsz;
2393
                        if (isIndirect) begin
2394
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2395
                                load_what2 <= `LW_BL;
2396
                                radr <= NdxAddr;
2397
                                next_state(LOAD1);
2398
                        end
2399
                        else begin
2400
                                b <= 24'd0;
2401
                                load_what <= `LW_BL;
2402
                                radr <= NdxAddr;
2403
                                next_state(LOAD1);
2404
                        end
2405
                end
2406 22 robfinch
        `MULD_NDX:
2407
                begin
2408
                        pc <= pc + insnsz;
2409
                        if (isIndirect) begin
2410
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2411
                                load_what2 <= `LW_BH;
2412
                                radr <= NdxAddr;
2413
                                next_state(LOAD1);
2414
                        end
2415
                        else begin
2416
                                b <= 24'd0;
2417
                                load_what <= `LW_BH;
2418
                                radr <= NdxAddr;
2419
                                next_state(LOAD1);
2420
                        end
2421
                end
2422 21 robfinch
`endif
2423
`ifdef SUPPORT_6309
2424
        `ADDW_NDX,`CMPW_NDX,`LDW_NDX,`SUBW_NDX,
2425
`endif
2426 2 robfinch
        `SUBD_NDX,`ADDD_NDX,`LDD_NDX,`CMPD_NDX,`ADCD_NDX,`SBCD_NDX:
2427
                begin
2428
                        pc <= pc + insnsz;
2429
                        if (isIndirect) begin
2430
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2431
                                load_what2 <= `LW_BH;
2432
                                radr <= NdxAddr;
2433
                                next_state(LOAD1);
2434
                        end
2435
                        else begin
2436
                                load_what <= `LW_BH;
2437
                                radr <= NdxAddr;
2438
                                next_state(LOAD1);
2439
                        end
2440
                end
2441
        `CMPX_NDX,`LDX_NDX,`LDU_NDX,`LDS_NDX,
2442
        `CMPY_NDX,`CMPS_NDX,`CMPU_NDX,`LDY_NDX:
2443
                begin
2444
                        pc <= pc + insnsz;
2445
                        if (isIndirect) begin
2446
                                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2447
                                load_what2 <= `LW_BH;
2448
                                radr <= NdxAddr;
2449
                                next_state(LOAD1);
2450
                        end
2451
                        else begin
2452
                                load_what <= `LW_BH;
2453
                                radr <= NdxAddr;
2454
                                next_state(LOAD1);
2455
                        end
2456
                end
2457
        `CLR_NDX:
2458
                begin
2459
                        res12 <= 13'h000;
2460
                        indexed_store(`SW_RES8);
2461
                end
2462
        `STA_NDX:       indexed_store(`SW_ACCA);
2463
        `STB_NDX:       indexed_store(`SW_ACCB);
2464
        `STD_NDX:       indexed_store(`SW_ACCDH);
2465
        `STU_NDX:       indexed_store(`SW_USPH);
2466
        `STS_NDX:       indexed_store(`SW_SSPH);
2467
        `STX_NDX:       indexed_store(`SW_XH);
2468
        `STY_NDX:       indexed_store(`SW_YH);
2469 21 robfinch
`ifdef SUPPORT_6309
2470
        `STW_NDX:       indexed_store(`SW_ACCWH);
2471
        `STE_NDX:       indexed_store(`SW_ACCE);
2472
        `STF_NDX:       indexed_store(`SW_ACCF);
2473
                `AIM_DP,`EIM_DP,`OIM_DP,`TIM_DP:
2474
                        begin
2475
                                load_what <= `LW_BL;
2476
                                pc <= pc + 4'd3;
2477
                                radr <= dp_address;
2478
                                next_state(LOAD1);
2479
                        end
2480
                `AIM_NDX,`EIM_NDX,`OIM_NDX,`TIM_NDX:
2481
                        begin
2482
                                pc <= pc + insnsz + 4'd1;
2483
                                if (isIndirect) begin
2484
                                        load_what <= isFar ? `LW_IA2316 : `LW_IAH;
2485
                                        load_what2 <= `LW_BL;
2486
                                        radr <= NdxAddr;
2487
                                        next_state(LOAD1);
2488
                                end
2489
                                else begin
2490
                                        b <= 'd0;
2491
                                        load_what <= `LW_BL;
2492
                                        radr <= NdxAddr;
2493
                                        next_state(LOAD1);
2494
                                end
2495
                        end
2496
`endif
2497
`ifdef SUPPORT_6309
2498
        `AIM_EXT,`OIM_EXT,`EIM_EXT,`TIM_EXT,
2499
`endif
2500 2 robfinch
        // Extended mode instructions
2501
        `NEG_EXT,`COM_EXT,`LSR_EXT,`ROR_EXT,`ASR_EXT,`ASL_EXT,`ROL_EXT,`DEC_EXT,`INC_EXT,`TST_EXT:
2502
                begin
2503
                        load_what <= `LW_BL;
2504
                        radr <= ex_address;
2505
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2506
                        next_state(LOAD1);
2507
                end
2508 21 robfinch
`ifdef SUPPORT_6309
2509
        `CMPE_EXT,`CMPF_EXT,
2510
        `LDE_EXT,`LDF_EXT,
2511
        `SUBE_EXT,`SUBF_EXT,
2512
`endif
2513 2 robfinch
        `SUBA_EXT,`CMPA_EXT,`SBCA_EXT,`ANDA_EXT,`BITA_EXT,`LDA_EXT,`EORA_EXT,`ADCA_EXT,`ORA_EXT,`ADDA_EXT,
2514
        `SUBB_EXT,`CMPB_EXT,`SBCB_EXT,`ANDB_EXT,`BITB_EXT,`LDB_EXT,`EORB_EXT,`ADCB_EXT,`ORB_EXT,`ADDB_EXT:
2515
                begin
2516
                        load_what <= `LW_BL;
2517
                        radr <= ex_address;
2518
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2519
                        next_state(LOAD1);
2520
                end
2521 21 robfinch
`ifdef SUPPORT_6309
2522
        `BITD_EXT,
2523
        `ANDD_EXT,
2524
        `ORD_EXT,
2525 22 robfinch
        `DIVD_EXT,
2526
        `DIVQ_EXT,
2527 21 robfinch
        `EORD_EXT:
2528
                begin
2529
                        load_what <= `LW_BL;
2530
                        radr <= ex_address;
2531
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2532
                        next_state(LOAD1);
2533
                end
2534
`endif
2535
`ifdef SUPPORT_6309
2536 22 robfinch
        `MULD_EXT,
2537 21 robfinch
        `ADDW_EXT,`CMPW_EXT,`LDW_EXT,`SUBW_EXT,
2538
`endif
2539 2 robfinch
        `SUBD_EXT,`ADDD_EXT,`LDD_EXT,`CMPD_EXT,`ADCD_EXT,`SBCD_EXT:
2540
                begin
2541
                        load_what <= `LW_BH;
2542
                        radr <= ex_address;
2543
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2544
                        next_state(LOAD1);
2545
                end
2546
        `CMPX_EXT,`LDX_EXT,`LDU_EXT,`LDS_EXT,
2547
        `CMPY_EXT,`CMPS_EXT,`CMPU_EXT,`LDY_EXT:
2548
                begin
2549
                        load_what <= `LW_BH;
2550
                        radr <= ex_address;
2551
                        pc <= pc + (isFar ? 32'd4 : 32'd3);
2552
                        next_state(LOAD1);
2553
                end
2554
        `CLR_EXT:
2555
                begin
2556
                        ex_store(`SW_RES8);
2557
                        res12 <= 13'h000;
2558
                end
2559
        `STA_EXT:       ex_store(`SW_ACCA);
2560
        `STB_EXT:       ex_store(`SW_ACCB);
2561
        `STD_EXT:       ex_store(`SW_ACCDH);
2562
        `STU_EXT:       ex_store(`SW_USPH);
2563
        `STS_EXT:       ex_store(`SW_SSPH);
2564
        `STX_EXT:       ex_store(`SW_XH);
2565
        `STY_EXT:       ex_store(`SW_YH);
2566 21 robfinch
`ifdef SUPPORT_6309
2567
        `STW_EXT:       ex_store(`SW_ACCWH);
2568
        `STE_EXT:       ex_store(`SW_ACCE);
2569
        `STF_EXT:       ex_store(`SW_ACCF);
2570
`endif
2571 2 robfinch
        `BSR:
2572
                begin
2573
                        store_what <= `SW_PCH;
2574
                        wadr <= ssp - 2'd2;
2575
                        ssp <= ssp - 2'd2;
2576
                        pc <= pc + 2'd2;
2577
                        next_state(STORE1);
2578
                end
2579
        `LBSR:
2580
                begin
2581
                        store_what <= `SW_PCH;
2582
                        wadr <= ssp - 2'd2;
2583
                        ssp <= ssp - 2'd2;
2584
                        pc <= pc + 2'd3;
2585
                        next_state(STORE1);
2586
                end
2587
        `JSR_DP:
2588
                begin
2589
                        store_what <= `SW_PCH;
2590
                        wadr <= ssp - 2'd2;
2591
                        ssp <= ssp - 2'd2;
2592
                        pc <= pc + 2'd2;
2593
                        next_state(STORE1);
2594
                end
2595
        `JSR_NDX:
2596
                begin
2597 15 robfinch
            if (isFar) begin
2598
                                store_what <= `SW_PC2316;
2599
                    wadr <= ssp - 16'd3;
2600
                    ssp <= ssp - 16'd3;
2601
            end
2602
                        begin
2603
                                store_what <= `SW_PCH;
2604
                                wadr <= ssp - 2'd2;
2605
                                ssp <= ssp - 2'd2;
2606 2 robfinch
                        end
2607
                        pc <= pc + insnsz;
2608
                        next_state(STORE1);
2609
                end
2610
        `JSR_EXT:
2611
                begin
2612
                        begin
2613
                                store_what <= `SW_PCH;
2614
                                wadr <= ssp - 2'd2;
2615
                                ssp <= ssp - 2'd2;
2616
                        end
2617
                        pc <= pc + 2'd3;
2618
                        next_state(STORE1);
2619
                end
2620
        `JSR_FAR:
2621
                begin
2622
                        store_what <= `SW_PC2316;
2623
                        wadr <= ssp - 16'd4;
2624
                        ssp <= ssp - 16'd4;
2625
                        pc <= pc + 32'd4;
2626
                        next_state(STORE1);
2627
                end
2628
        `RTS:
2629
                begin
2630
                        load_what <= `LW_PCH;
2631
                        radr <= ssp;
2632
                        next_state(LOAD1);
2633
                end
2634
        `RTF:
2635
                begin
2636
                        load_what <= `LW_PC2316;
2637
                        radr <= ssp;
2638
                        next_state(LOAD1);
2639
                end
2640
        `JMP_DP:        pc <= dp_address;
2641
        `JMP_EXT:       pc <= address;
2642
        `JMP_FAR:       pc <= far_address;
2643
        `JMP_NDX:
2644
                begin
2645
                        if (isIndirect) begin
2646
                        radr <= NdxAddr;
2647
                            if (isFar)
2648
                                   load_what <= `LW_PC2316;
2649
                            else
2650
                                   load_what <= `LW_PCH;
2651
                                next_state(LOAD1);
2652
                        end
2653
                        else
2654
                                pc <= isFar ? NdxAddr : {pc[`BYTE3],NdxAddr[`DBLBYTE]};
2655
                end
2656
        `LEAX_NDX,`LEAY_NDX,`LEAS_NDX,`LEAU_NDX:
2657
                begin
2658
                        pc <= pc + insnsz;
2659
                        if (isIndirect) begin
2660
                                load_what <= `LW_IAH;
2661
                                radr <= NdxAddr;
2662
                                state <= LOAD1;
2663
                        end
2664
                        else
2665
                                res <= NdxAddr[`DBLBYTE];
2666
                end
2667
        `PSHU,`PSHS:
2668
                begin
2669
                        next_state(PUSH1);
2670
                        pc <= pc + 2'd2;
2671
                end
2672
        `PULS:
2673
                begin
2674
                        radr <= ssp;
2675
                        next_state(PULL1);
2676
                        pc <= pc + 2'd2;
2677
                end
2678
        `PULU:
2679
                begin
2680
                        radr <= {usppg,8'h00} + usp;
2681
                        next_state(PULL1);
2682
                        pc <= pc + 2'd2;
2683
                end
2684
        `BEQ,`BNE,`BMI,`BPL,`BVS,`BVC,`BHI,`BLS,`BHS,`BLO,`BGT,`BGE,`BLT,`BLE,`BRA,`BRN:
2685
                if (takb)
2686
                        pc <= pc + {{24{ir[BPBX2M1]}},ir[`HIBYTE]} + 2'd2;
2687
                else
2688
                        pc <= pc + 2'd2;
2689
        // PC is already incremented by one due to the PG10 prefix.
2690
        `LBEQ,`LBNE,`LBMI,`LBPL,`LBVS,`LBVC,`LBHI,`LBLS,`LBHS,`LBLO,`LBGT,`LBGE,`LBLT,`LBLE,`LBRN:
2691
                if (takb)
2692
                        pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]} + 2'd3;
2693
                else
2694
                        pc <= pc + 2'd3;
2695
        `LBRA:  pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]} + 2'd3;
2696
        `RTI:
2697
                begin
2698
                        load_what <= `LW_CCR;
2699
                        radr <= ssp;
2700
                        isFar <= `TRUE;
2701
                        next_state(LOAD1);
2702
                end
2703
        `SWI:
2704
                begin
2705
                        im <= 1'b1;
2706
                        firqim <= 1'b1;
2707
                        ir[`LOBYTE] <= `INT;
2708
                        ipg <= 2'b11;
2709
                        vect <= `SWI_VECT;
2710
                        next_state(DECODE);
2711
                end
2712
        `SWI2:
2713
                begin
2714
                        ir[`LOBYTE] <= `INT;
2715
                        ipg <= 2'b11;
2716
                        vect <= `SWI2_VECT;
2717
                        next_state(DECODE);
2718
                end
2719
        `SWI3:
2720
                begin
2721
                        ir[`LOBYTE] <= `INT;
2722
                        ipg <= 2'b11;
2723
                        vect <= `SWI3_VECT;
2724
                        next_state(DECODE);
2725
                end
2726
        // If the processor was in the wait state before the interrupt occurred
2727
        // the registers will have already been pushed. All that needs to be
2728
        // done is to vector to the interrupt routine.
2729
        `INT:
2730
                begin
2731
                        if (wait_state) begin
2732
                                wait_state <= `FALSE;
2733
                                if (vec_i != 24'h0) begin
2734 22 robfinch
                            pc <= vec_i;
2735
                            next_state(IFETCH);
2736 2 robfinch
                                end
2737
                                else begin
2738 22 robfinch
                            radr <= vect;
2739
                load_what <= `LW_PCH;
2740
                            pc <= 32'hFFFFFFFE;
2741
                            next_state(LOAD1);
2742 2 robfinch
                                end
2743
                        end
2744
                        else begin
2745
                                if (isNMI | isIRQ | isSWI | isSWI2 | isSWI3) begin
2746 22 robfinch
                                        ir[`HIBYTE] <= natMd ? 12'hFFF : 12'h0FF;
2747 2 robfinch
                                        ef <= 1'b1;
2748
                                end
2749
                                else if (isFIRQ) begin
2750
                                        if (natMd) begin
2751
                                                ef <= firqMd;
2752 22 robfinch
                                                ir[`HIBYTE] <= firqMd ? 12'hFFF : 12'h081;
2753 2 robfinch
                                        end
2754
                                        else begin
2755 22 robfinch
                                                ir[`HIBYTE] <= 12'h081;
2756 2 robfinch
                                                ef <= 1'b0;
2757
                                        end
2758
                                end
2759
                                pc <= pc;
2760
                                isFar <= `TRUE;
2761
                                next_state(PUSH1);
2762
                        end
2763
                end
2764 22 robfinch
        default:
2765
                if (natMd) begin
2766
                        iop <= 1'b1;
2767
                        bs_o <= 1'b1;
2768
                        ir[`LOBYTE] <= `INT;
2769
                        ipg <= 2'b11;
2770
                        vect <= `IOP_VECT;
2771
                        next_state(DECODE);
2772
                end
2773 2 robfinch
        endcase
2774
end
2775
endtask
2776
 
2777
// ============================================================================
2778
// MEMORY LOAD
2779
// ============================================================================
2780
task tLoad1;
2781
begin
2782
`ifdef SUPPORT_DCACHE
2783
        if (unCachedData)
2784
`endif
2785
        case(radr)
2786 22 robfinch
        `CORENO:                load_tsk({2'b0,id});
2787
        `CHKPOINT:      load_tsk(chkpoint);
2788
        `MSCOUNT+0:     load_tsk(12'h0);
2789
        `MSCOUNT+1:     load_tsk(ms_count[35:24]);
2790
        `MSCOUNT+2:     load_tsk(ms_count[23:12]);
2791
        `MSCOUNT+3:     load_tsk(ms_count[11: 0]);
2792
`ifdef SUPPORT_DEBUG_REG
2793
        `BRKAD0+0:              load_tsk(brkad[0][`BYTE2]);
2794
        `BRKAD0+1:              load_tsk(brkad[0][`BYTE1]);
2795
        `BRKAD1+0:              load_tsk(brkad[1][`BYTE2]);
2796
        `BRKAD1+1:              load_tsk(brkad[1][`BYTE1]);
2797
        `BRKAD2+0:              load_tsk(brkad[2][`BYTE2]);
2798
        `BRKAD2+1:              load_tsk(brkad[2][`BYTE1]);
2799
        `BRKAD3+0:              load_tsk(brkad[3][`BYTE2]);
2800
        `BRKAD3+1:              load_tsk(brkad[3][`BYTE1]);
2801
        `BRKCTRL0:              load_tsk(brkctrl[0]);
2802
        `BRKCTRL1:              load_tsk(brkctrl[1]);
2803
        `BRKCTRL2:              load_tsk(brkctrl[2]);
2804
        `BRKCTRL3:              load_tsk(brkctrl[3]);
2805
`endif
2806 2 robfinch
        default:
2807 22 robfinch
`ifdef SUPPORT_DEBUG_REG
2808
        if (brkctrl[0].en && brkctrl[0].match_type==BMT_LS && (radr & {{20{1'b1}},brkctrl[0].amask})==brkad[0]) begin
2809
                brkctrl[0].hit <= 1'b1;
2810
                bs_o <= 1'b1;
2811
                ir[`LOBYTE] <= `INT;
2812
                ipg <= 2'b11;
2813
                vect <= `DBG_VECT;
2814
                next_state(DECODE);
2815
        end
2816
        else if (brkctrl[1].en && brkctrl[1].match_type==BMT_LS && (radr & {{20{1'b1}},brkctrl[1].amask})==brkad[1]) begin
2817
                brkctrl[1].hit <= 1'b1;
2818
                bs_o <= 1'b1;
2819
                ir[`LOBYTE] <= `INT;
2820
                ipg <= 2'b11;
2821
                vect <= `DBG_VECT;
2822
                next_state(DECODE);
2823
        end
2824
        else if (brkctrl[2].en && brkctrl[2].match_type==BMT_LS && (radr & {{20{1'b1}},brkctrl[2].amask})==brkad[2]) begin
2825
                brkctrl[2].hit <= 1'b1;
2826
                bs_o <= 1'b1;
2827
                ir[`LOBYTE] <= `INT;
2828
                ipg <= 2'b11;
2829
                vect <= `DBG_VECT;
2830
                next_state(DECODE);
2831
        end
2832
        else if (brkctrl[3].en && brkctrl[3].match_type==BMT_LS && (radr & {{20{1'b1}},brkctrl[3].amask})==brkad[3]) begin
2833
                brkctrl[3].hit <= 1'b1;
2834
                bs_o <= 1'b1;
2835
                ir[`LOBYTE] <= `INT;
2836
                ipg <= 2'b11;
2837
                vect <= `DBG_VECT;
2838
                next_state(DECODE);
2839
        end
2840
        else
2841
`endif
2842 2 robfinch
        if (~ack_i) begin
2843
                lock_o <= lock_bus;
2844
                wb_read(radr);
2845
                if (!tsc)
2846
                        next_state(LOAD2);
2847
        end
2848
`ifdef SUPPORT_DCACHE
2849
        else if (dhit)
2850
                load_tsk(rdat);
2851
        else begin
2852
                retstate <= LOAD1;
2853
                state <= DCACHE1;
2854
        end
2855
`endif
2856
        endcase
2857
end
2858
endtask
2859
 
2860
task tLoad2;
2861
begin
2862
        // On a tri-state condition abort the bus cycle and retry the load.
2863
        if (tsc|rty_i|bto) begin
2864
                wb_nack();
2865
                next_state(LOAD1);
2866
        end
2867
        else if (ack_i) begin
2868
                wb_nack();
2869
                load_tsk(dati);
2870
        end
2871
`ifdef SUPPORT_BERR
2872
        else if (err_i) begin
2873
                lock_o <= 1'b0;
2874
                wb_nack();
2875
                derr_address <= adr_o;
2876
//              intno <= 9'd508;
2877
                state <= BUS_ERROR;
2878
        end
2879
`endif
2880
end
2881
endtask
2882
 
2883
// ============================================================================
2884
// EXECUTE
2885
//
2886
// Perform calculations
2887
// ============================================================================
2888
task tExecute;
2889
begin
2890
        next_state(IFETCH);
2891
        case(ir12)
2892 22 robfinch
        `SUBD_IMM,
2893 2 robfinch
        `SUBD_DP,`SUBD_NDX,`SUBD_EXT,
2894
        `CMPD_DP,`CMPD_NDX,`CMPD_EXT:
2895 22 robfinch
                if (dm)
2896
                        res <= bcdsubo;
2897
                else begin
2898 2 robfinch
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2899
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - b[`DBLBYTE];
2900
                end
2901 22 robfinch
        `SBCD_IMM,`SBCD_DP,`SBCD_NDX,`SBCD_EXT:
2902
                if (dm)
2903
                        res <= bcdsubo;
2904
                else begin
2905 2 robfinch
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2906
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} - b[`DBLBYTE] - {23'b0,cf};
2907
                end
2908 22 robfinch
        `ADDD_IMM,`ADDD_DP,`ADDD_NDX,`ADDD_EXT:
2909
                if (dm)
2910
                        res <= bcdaddo;
2911
                else begin
2912 2 robfinch
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2913
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + b[`DBLBYTE];
2914
                end
2915 21 robfinch
`ifdef SUPPORT_6309
2916
        `SUBW_DP,`SUBW_NDX,`SUBW_EXT,
2917
        `CMPW_DP,`CMPW_NDX,`CMPW_EXT:
2918
                begin
2919
                    a <= {acce[`LOBYTE],accf[`LOBYTE]};
2920
                        res <= {acce[`LOBYTE],accf[`LOBYTE]} - b[`DBLBYTE];
2921
                end
2922
        `ADDW_DP,`ADDW_NDX,`ADDW_EXT:
2923
                begin
2924
                    a <= {acce[`LOBYTE],accf[`LOBYTE]};
2925
                        res <= {acce[`LOBYTE],accf[`LOBYTE]} + b[`DBLBYTE];
2926
                end
2927
        `LDW_DP,`LDW_NDX,`LDW_EXT:
2928
                res <= b[`DBLBYTE];
2929
`endif
2930 22 robfinch
        `ADCD_IMM,`ADCD_DP,`ADCD_NDX,`ADCD_EXT:
2931
                if (dm)
2932
                        res <= bcdaddo;
2933
                else begin
2934 2 robfinch
                    a <= {acca[`LOBYTE],accb[`LOBYTE]};
2935
                        res <= {acca[`LOBYTE],accb[`LOBYTE]} + b[`DBLBYTE] + {23'b0,cf};
2936
                end
2937
        `LDD_DP,`LDD_NDX,`LDD_EXT:
2938
                res <= b[`DBLBYTE];
2939 21 robfinch
`ifdef SUPPORT_6309
2940
        `CMPE_DP,`CMPE_NDX,`CMPE_EXT,
2941
        `CMPF_DP,`CMPF_NDX,`CMPF_EXT,
2942
        `SUBE_DP,`SUBE_NDX,`SUBE_EXT,
2943 22 robfinch
        `SUBF_DP,`SUBF_NDX,`SUBF_EXT:
2944 2 robfinch
                begin
2945
                        a <= acc;
2946
           res12 <= acc[`LOBYTE] - b12;
2947
                        end
2948 22 robfinch
`endif
2949
        `CMPA_IMM,`CMPA_DP,`CMPA_NDX,`CMPA_EXT,
2950
        `SUBA_IMM,`SUBA_DP,`SUBA_NDX,`SUBA_EXT,
2951
        `CMPB_IMM,`CMPB_DP,`CMPB_NDX,`CMPB_EXT,
2952
        `SUBB_IMM,`SUBB_DP,`SUBB_NDX,`SUBB_EXT:
2953
                if (dm)
2954
                        res12 <= bcdsubbo;
2955
                else begin
2956
        a <= acc;
2957
        res12 <= acc[`LOBYTE] - b12;
2958
                end
2959 2 robfinch
 
2960 22 robfinch
        `SBCA_IMM,`SBCA_DP,`SBCA_NDX,`SBCA_EXT,
2961
        `SBCB_IMM,`SBCB_DP,`SBCB_NDX,`SBCB_EXT:
2962
                if (dm)
2963
                        res12 <= bcdsubbo;
2964
          else begin
2965
        a <= acc;
2966
    res12 <= acc[`LOBYTE] - b12 - cf;
2967
    end
2968 2 robfinch
        `BITA_DP,`BITA_NDX,`BITA_EXT,
2969
        `ANDA_DP,`ANDA_NDX,`ANDA_EXT,
2970
        `BITB_DP,`BITB_NDX,`BITB_EXT,
2971
        `ANDB_DP,`ANDB_NDX,`ANDB_EXT:
2972
                                res12 <= acc[`LOBYTE] & b12;
2973 21 robfinch
`ifdef SUPPORT_6309
2974
        `BITD_DP,`BITD_NDX,`BITD_EXT,
2975
        `ANDD_DP,`ANDD_NDX,`ANDD_EXT:
2976
                res <= {acca[`LOBYTE],accb[`LOBYTE]} & b[`DBLBYTE];
2977 22 robfinch
        `DIVQ_DP,`DIVQ_NDX,`DIVQ_EXT,
2978
        `DIVD_DP,`DIVD_NDX,`DIVD_EXT:
2979
                if (b==24'd0) begin
2980
                        dbz <= 1'b1;
2981
                        bs_o <= 1'b1;
2982
                        ir[`LOBYTE] <= `INT;
2983
                        ipg <= 2'b11;
2984
                        vect <= `IOP_VECT;
2985
                        next_state(DECODE);
2986
                end
2987
                else
2988
                        next_state(DIV1);
2989
        `MULD_DP,`MULD_NDX,`MULD_EXT:
2990
                begin
2991
                        divcnt <= 6'd7;
2992
                        next_state(MUL2);
2993
                end
2994 21 robfinch
        `EORD_DP,`EORD_NDX,`EORD_EXT:
2995
                res <= {acca[`LOBYTE],accb[`LOBYTE]} ^ b[`DBLBYTE];
2996
        `ORD_DP,`ORD_NDX,`ORD_EXT:
2997
                res <= {acca[`LOBYTE],accb[`LOBYTE]} | b[`DBLBYTE];
2998
        `LDE_DP,`LDE_NDX,`LDE_EXT,
2999
        `LDF_DP,`LDF_NDX,`LDF_EXT:
3000
                res12 <= b12;
3001
`endif
3002 2 robfinch
        `LDA_DP,`LDA_NDX,`LDA_EXT,
3003
        `LDB_DP,`LDB_NDX,`LDB_EXT:
3004
                        res12 <= b12;
3005
        `EORA_DP,`EORA_NDX,`EORA_EXT,
3006
        `EORB_DP,`EORB_NDX,`EORB_EXT:
3007
                                res12 <= acc[`LOBYTE] ^ b12;
3008 22 robfinch
        `ADCA_IMM,`ADCA_DP,`ADCA_NDX,`ADCA_EXT,
3009
        `ADCB_IMM,`ADCB_DP,`ADCB_NDX,`ADCB_EXT:
3010
                if (dm)
3011
                        res12 <= bcdaddbo;
3012
                else begin
3013
                    a <= acc;
3014
                        res12 <= acc[`LOBYTE] + b12 + cf;
3015
                end
3016 2 robfinch
        `ORA_DP,`ORA_NDX,`ORA_EXT,
3017
        `ORB_DP,`ORB_NDX,`ORB_EXT:
3018
                                res12 <= acc[`LOBYTE] | b12;
3019 21 robfinch
`ifdef SUPPORT_6309
3020
        `ADDE_DP,`ADDE_NDX,`ADDE_EXT,
3021
        `ADDF_DP,`ADDF_NDX,`ADDF_EXT,
3022
`endif
3023 22 robfinch
        `ADDA_IMM,`ADDA_DP,`ADDA_NDX,`ADDA_EXT,
3024
        `ADDB_IMM,`ADDB_DP,`ADDB_NDX,`ADDB_EXT:
3025
                if (dm)
3026
                        res12 <= bcdaddbo;
3027
                else begin
3028
            a <= acc;
3029
      res12 <= acc[`LOBYTE] + b12;
3030
        end
3031 2 robfinch
 
3032
        `LDU_DP,`LDS_DP,`LDX_DP,`LDY_DP,
3033
        `LDU_NDX,`LDS_NDX,`LDX_NDX,`LDY_NDX,
3034
        `LDU_EXT,`LDS_EXT,`LDX_EXT,`LDY_EXT:    res <= b[`DBLBYTE];
3035
        `CMPX_DP,`CMPX_NDX,`CMPX_EXT:   begin a <= xr; res <= xr[`DBLBYTE] - b[`DBLBYTE]; end
3036
        `CMPY_DP,`CMPY_NDX,`CMPY_EXT:   begin a <= yr; res <= yr[`DBLBYTE] - b[`DBLBYTE]; end
3037
        `CMPS_DP,`CMPS_NDX,`CMPS_EXT:   begin a <= ssp; res <= ssp[`DBLBYTE] - b[`DBLBYTE]; end
3038
        `CMPU_DP,`CMPU_NDX,`CMPU_EXT:   begin a <= usp; res <= usp[`DBLBYTE] - b[`DBLBYTE]; end
3039
 
3040
        `NEG_DP,`NEG_NDX,`NEG_EXT:      begin res12 <= -b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3041 22 robfinch
        `NEGA,`NEGB:    begin res12 <= bcdnegbo; end
3042 2 robfinch
        `COM_DP,`COM_NDX,`COM_EXT:      begin res12 <= ~b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3043
        `LSR_DP,`LSR_NDX,`LSR_EXT:      begin res12 <= {b[0],1'b0,b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
3044
        `ROR_DP,`ROR_NDX,`ROR_EXT:      begin res12 <= {b[0],cf,b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
3045
        `ASR_DP,`ASR_NDX,`ASR_EXT:      begin res12 <= {b[0],b[BPBM1],b[BPBM1:1]}; store_what <= `SW_RES8; wadr <= radr; next_state(STORE1); end
3046
        `ASL_DP,`ASL_NDX,`ASL_EXT:      begin res12 <= {b12,1'b0}; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3047
        `ROL_DP,`ROL_NDX,`ROL_EXT:      begin res12 <= {b12,cf}; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3048
        `DEC_DP,`DEC_NDX,`DEC_EXT:      begin res12 <= b12 - 2'd1; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3049
        `INC_DP,`INC_NDX,`INC_EXT:      begin res12 <= b12 + 2'd1; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3050
        `TST_DP,`TST_NDX,`TST_EXT:      res12 <= b12;
3051 21 robfinch
`ifdef SUPPORT_6309
3052 22 robfinch
        `NEGD:  begin res <= bcdnego; end
3053 2 robfinch
        `AIM_DP,`AIM_NDX,`AIM_EXT:      begin res12 <= ir[`HIBYTE] & b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3054
        `OIM_DP,`OIM_NDX,`OIM_EXT:      begin res12 <= ir[`HIBYTE] | b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3055
        `EIM_DP,`EIM_NDX,`OIM_EXT:  begin res12 <= ir[`HIBYTE] ^ b12; wadr <= radr; store_what <= `SW_RES8; next_state(STORE1); end
3056
        `TIM_DP,`TIM_NDX,`TIM_EXT:      begin res12 <= ir[`HIBYTE] & b12; end
3057 21 robfinch
`endif
3058 2 robfinch
        default:        ;
3059
        endcase
3060
end
3061
endtask
3062
 
3063
// ============================================================================
3064
// MEMORY STORE
3065
// ============================================================================
3066
 
3067
task tStore1;
3068
begin
3069
        if (!ack_i) begin
3070
                lock_o <= lock_bus;
3071
`ifdef SUPPORT_CHECKPOINT
3072 22 robfinch
                if (wadr==CHKPOINT)
3073 2 robfinch
                        next_state(IFETCH);
3074
                else
3075
`endif
3076 22 robfinch
`ifdef SUPPORT_DEBUG_REG
3077
        if (brkctrl[0].en && brkctrl[0].match_type==BMT_DS && (radr & {{20{1'b1}},brkctrl[0].amask})==brkad[0]) begin
3078
                brkctrl[0].hit <= 1'b1;
3079
                bs_o <= 1'b1;
3080
                ir[`LOBYTE] <= `INT;
3081
                ipg <= 2'b11;
3082
                vect <= `DBG_VECT;
3083
                next_state(DECODE);
3084
        end
3085
        else if (brkctrl[1].en && brkctrl[1].match_type==BMT_DS && (radr & {{20{1'b1}},brkctrl[1].amask})==brkad[1]) begin
3086
                brkctrl[1].hit <= 1'b1;
3087
                bs_o <= 1'b1;
3088
                ir[`LOBYTE] <= `INT;
3089
                ipg <= 2'b11;
3090
                vect <= `DBG_VECT;
3091
                next_state(DECODE);
3092
        end
3093
        else if (brkctrl[2].en && brkctrl[2].match_type==BMT_DS && (radr & {{20{1'b1}},brkctrl[2].amask})==brkad[2]) begin
3094
                brkctrl[2].hit <= 1'b1;
3095
                bs_o <= 1'b1;
3096
                ir[`LOBYTE] <= `INT;
3097
                ipg <= 2'b11;
3098
                vect <= `DBG_VECT;
3099
                next_state(DECODE);
3100
        end
3101
        else if (brkctrl[3].en && brkctrl[3].match_type==BMT_DS && (radr & {{20{1'b1}},brkctrl[3].amask})==brkad[3]) begin
3102
                brkctrl[3].hit <= 1'b1;
3103
                bs_o <= 1'b1;
3104
                ir[`LOBYTE] <= `INT;
3105
                ipg <= 2'b11;
3106
                vect <= `DBG_VECT;
3107
                next_state(DECODE);
3108
        end
3109
        else
3110
`endif
3111 2 robfinch
                begin
3112
                        case(store_what)
3113
                        `SW_ACCDH:      wb_write(wadr,acca[`LOBYTE]);
3114
                        `SW_ACCDL:      wb_write(wadr,accb[`LOBYTE]);
3115
                        `SW_ACCA:       wb_write(wadr,acca[`LOBYTE]);
3116
                        `SW_ACCB:       wb_write(wadr,accb[`LOBYTE]);
3117 21 robfinch
`ifdef SUPPORT_6309
3118
                        `SW_ACCWH:      wb_write(wadr,acce[`LOBYTE]);
3119
                        `SW_ACCWL:      wb_write(wadr,accf[`LOBYTE]);
3120
                        `SW_ACCE:       wb_write(wadr,acce[`LOBYTE]);
3121
                        `SW_ACCF:       wb_write(wadr,accf[`LOBYTE]);
3122
`endif
3123 2 robfinch
                        `SW_DPR:        wb_write(wadr,dpr);
3124
                        `SW_XL: wb_write(wadr,xr[`LOBYTE]);
3125
                        `SW_XH: wb_write(wadr,xr[`HIBYTE]);
3126
                        `SW_YL: wb_write(wadr,yr[`LOBYTE]);
3127
                        `SW_YH: wb_write(wadr,yr[`HIBYTE]);
3128
                        `SW_USPL:       wb_write(wadr,usp[`LOBYTE]);
3129
                        `SW_USPH:       wb_write(wadr,usp[`HIBYTE]);
3130
                        `SW_SSPL:       wb_write(wadr,ssp[`LOBYTE]);
3131
                        `SW_SSPH:       wb_write(wadr,ssp[`HIBYTE]);
3132
                        `SW_PC2316:     wb_write(wadr,pc[`BYTE3]);
3133
                        `SW_PCH:        wb_write(wadr,pc[`HIBYTE]);
3134
                        `SW_PCL:        wb_write(wadr,pc[`LOBYTE]);
3135
                        `SW_CCR:        wb_write(wadr,ccr);
3136
                        `SW_RES8:       wb_write(wadr,res12[`LOBYTE]);
3137
                        `SW_RES16H:     wb_write(wadr,res[`HIBYTE]);
3138
                        `SW_RES16L:     wb_write(wadr,res[`LOBYTE]);
3139
                        `SW_DEF8:       wb_write(wadr,wdat);
3140
                        default:        wb_write(wadr,wdat);
3141
                        endcase
3142
`ifdef SUPPORT_DCACHE
3143
                        radr <= wadr;           // Do a cache read to test the hit
3144
`endif
3145
                        if (!tsc)
3146 21 robfinch
                                next_state(STORE1a);
3147 2 robfinch
                end
3148
        end
3149
end
3150
endtask
3151
 
3152 21 robfinch
task tStore1a;
3153
begin
3154
        if (!tsc) begin
3155
                cyc_o <= 1'b1;
3156
                stb_o <= 1'b1;
3157
                next_state(STORE2);
3158
        end
3159
end
3160
endtask
3161
 
3162 2 robfinch
// Terminal state for stores. Update the data cache if there was a cache hit.
3163
// Clear any previously set lock status
3164
task tStore2;
3165
begin
3166
        // On a tri-state condition abort the bus cycle and retry the store.
3167
        if (tsc|rty_i|bto) begin
3168
                wb_nack();
3169
                next_state(STORE1);
3170
        end
3171
        else if (ack_i) begin
3172
                wb_nack();
3173
                wdat <= dat_o;
3174
                wadr <= wadr + 2'd1;
3175
                next_state(IFETCH);
3176
                case(store_what)
3177
                `SW_CCR:
3178
                        begin
3179
                                if (isINT) begin
3180
                                        im <= 1'b1;
3181
                                        firqim <= 1'b1;
3182
                                end
3183
                                next_state(PUSH2);
3184
                        end
3185
                `SW_ACCA:
3186
                        if (isINT | isPSHS | isPSHU)
3187
                                next_state(PUSH2);
3188
                        else    // STA
3189
                                next_state(IFETCH);
3190
                `SW_ACCB:
3191
                        if (isINT | isPSHS | isPSHU)
3192
                                next_state(PUSH2);
3193
                        else    // STB
3194
                                next_state(IFETCH);
3195 21 robfinch
`ifdef SUPPORT_6309
3196
                `SW_ACCE:
3197
                        if (isINT | isPSHS | isPSHU)
3198
                                next_state(PUSH2);
3199
                        else    // STE
3200
                                next_state(IFETCH);
3201
                `SW_ACCF:
3202
                        if (isINT | isPSHS | isPSHU)
3203
                                next_state(PUSH2);
3204
                        else    // STF
3205
                                next_state(IFETCH);
3206
                `SW_ACCWH:
3207
                        begin
3208
                                store_what <= `SW_ACCWL;
3209
                                next_state(STORE1);
3210
                        end
3211
                `SW_ACCWL:      next_state(IFETCH);
3212
`endif
3213 2 robfinch
                `SW_ACCDH:
3214
                        begin
3215
                                store_what <= `SW_ACCDL;
3216
                                next_state(STORE1);
3217
                        end
3218
                `SW_ACCDL:      next_state(IFETCH);
3219
                `SW_DPR:        next_state(PUSH2);
3220
                `SW_XH:
3221
                        begin
3222
                                store_what <= `SW_XL;
3223
                                next_state(STORE1);
3224
                        end
3225
                `SW_XL:
3226
                        if (isINT | isPSHS | isPSHU)
3227
                                next_state(PUSH2);
3228
                        else    // STX
3229
                                next_state(IFETCH);
3230
                `SW_YH:
3231
                        begin
3232
                                store_what <= `SW_YL;
3233
                                next_state(STORE1);
3234
                        end
3235
                `SW_YL:
3236
                        if (isINT | isPSHS | isPSHU)
3237
                                next_state(PUSH2);
3238
                        else    // STY
3239
                                next_state(IFETCH);
3240
                `SW_USPH:
3241
                        begin
3242
                                store_what <= `SW_USPL;
3243
                                next_state(STORE1);
3244
                        end
3245
                `SW_USPL:
3246
                        if (isINT | isPSHS | isPSHU)
3247
                                next_state(PUSH2);
3248
                        else    // STU
3249
                                next_state(IFETCH);
3250
                `SW_SSPH:
3251
                        begin
3252
                                store_what <= `SW_SSPL;
3253
                                next_state(STORE1);
3254
                        end
3255
                `SW_SSPL:
3256
                        if (isINT | isPSHS | isPSHU)
3257
                                next_state(PUSH2);
3258
                        else    // STS
3259
                                next_state(IFETCH);
3260
                `SW_PC2316:
3261
                        begin
3262
                                store_what <= `SW_PCH;
3263
                                next_state(STORE1);
3264
                        end
3265
                `SW_PCH:
3266
                        begin
3267
                                store_what <= `SW_PCL;
3268
                                next_state(STORE1);
3269
                        end
3270
                `SW_PCL:
3271
                        if (isINT | isPSHS | isPSHU)
3272
                                next_state(PUSH2);
3273
                        else begin      // JSR
3274
                                next_state(IFETCH);
3275
                                case(ir12)
3276
                                `BSR:           pc <= pc + {{24{ir[BPBX2M1]}},ir[`HIBYTE]};
3277
                                `LBSR:  pc <= pc + {{12{ir[BPB*3-1]}},ir[`HIBYTE],ir[`BYTE3]};
3278
                                `JSR_DP:        pc <= {dpr,ir[`HIBYTE]};
3279
                                `JSR_EXT:       pc <= {pc[`BYTE3],address[`DBLBYTE]};
3280
                                `JSR_FAR:
3281
                                        begin
3282
                                                pc <= far_address;
3283
                                                $display("Loading PC with %h", far_address);
3284
                                        end
3285
                                `JSR_NDX:
3286
                                        begin
3287
                                                if (isIndirect) begin
3288
                                                        radr <= NdxAddr;
3289
                                                        load_what <= isFar ? `LW_PC2316 : `LW_PCH;
3290
                                                        next_state(LOAD1);
3291
                                                end
3292
                                                else
3293
                                                        pc <= isFar ? NdxAddr : {pc[`BYTE3],NdxAddr[`DBLBYTE]};
3294
                                        end
3295
                                endcase
3296
                        end
3297
                endcase
3298
`ifdef SUPPORT_DCACHE
3299
                if (!dhit && write_allocate) begin
3300
                        state <= DCACHE1;
3301
                end
3302
`endif
3303
        end
3304
`ifdef SUPPORT_BERR
3305
        else if (err_i) begin
3306
                lock_o <= 1'b0;
3307
                wb_nack();
3308
                state <= BUS_ERROR;
3309
        end
3310
`endif
3311
end
3312
endtask
3313
 
3314
// ============================================================================
3315
// WRITEBACK
3316
//
3317
// Write results back to the register file and status flags.
3318
// Which registers and flags get updated depend on the instruction.
3319
// ============================================================================
3320
 
3321
task tWriteback;
3322
begin
3323
        if (first_ifetch) begin
3324
                first_ifetch <= `FALSE;
3325
                case(ir12)
3326
                `ABX:   xr <= res;
3327
                `ADDA_IMM,`ADDA_DP,`ADDA_NDX,`ADDA_EXT,
3328
                `ADCA_IMM,`ADCA_DP,`ADCA_NDX,`ADCA_EXT:
3329
                        begin
3330 22 robfinch
                                cf <= dm ? bcdaddbcf : (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
3331 2 robfinch
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3332
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
3333
                                nf <= res12[BPBM1];
3334
                                zf <= res12[`LOBYTE]==12'h000;
3335
                                acca <= res12[`LOBYTE];
3336
                        end
3337
                `ADDB_IMM,`ADDB_DP,`ADDB_NDX,`ADDB_EXT,
3338
                `ADCB_IMM,`ADCB_DP,`ADCB_NDX,`ADCB_EXT:
3339
                        begin
3340 22 robfinch
                                cf <= dm ? bcdaddbcf : (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
3341 2 robfinch
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3342
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
3343
                                nf <= res12[BPBM1];
3344
                                zf <= res12[`LOBYTE]==12'h000;
3345
                                accb <= res12[`LOBYTE];
3346
                        end
3347 21 robfinch
`ifdef SUPPORT_6309
3348
                `ADDE_IMM,`ADDE_DP,`ADDE_NDX,`ADDE_EXT:
3349
                        begin
3350
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
3351
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3352
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
3353
                                nf <= res12[BPBM1];
3354
                                zf <= res12[`LOBYTE]==12'h000;
3355
                                accf <= res12[`LOBYTE];
3356
                        end
3357
                `ADDF_IMM,`ADDF_DP,`ADDF_NDX,`ADDF_EXT:
3358
                        begin
3359
                                cf <= (a[BPBM1]&b[BPBM1])|(a[BPBM1]&~res12[BPBM1])|(b[BPBM1]&~res12[BPBM1]);
3360
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3361
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
3362
                                nf <= res12[BPBM1];
3363
                                zf <= res12[`LOBYTE]==12'h000;
3364
                                acce <= res12[`LOBYTE];
3365
                        end
3366
`endif
3367 2 robfinch
                `ADDD_IMM,`ADDD_DP,`ADDD_NDX,`ADDD_EXT:
3368
                        begin
3369 22 robfinch
                                cf <= dm ? bcdaddcf : (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
3370 2 robfinch
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
3371
                                nf <= res[BPBX2M1];
3372
                                zf <= res[`DBLBYTE]==24'h000000;
3373
                                acca <= res[`HIBYTE];
3374
                                accb <= res[`LOBYTE];
3375
                        end
3376 21 robfinch
`ifdef SUPPORT_6309
3377
                `ADDW_IMM,`ADDW_DP,`ADDW_NDX,`ADDW_EXT:
3378
                        begin
3379
                                cf <= (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
3380
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
3381
                                nf <= res[BPBX2M1];
3382
                                zf <= res[`DBLBYTE]==24'h000000;
3383
                                acce <= res[`HIBYTE];
3384
                                accf <= res[`LOBYTE];
3385
                        end
3386
                `ADCD_IMM,`ADCD_DP,`ADCD_NDX,`ADCD_EXT:
3387
                        begin
3388 22 robfinch
                                cf <= dm ? bcdaddcf : (a[BPBX2M1]&b[BPBX2M1])|(a[BPBX2M1]&~res[BPBX2M1])|(b[BPBX2M1]&~res[BPBX2M1]);
3389 21 robfinch
                                vf <= (res[BPBX2M1] ^ b[BPBX2M1]) & (1'b1 ^ a[BPBX2M1] ^ b[BPBX2M1]);
3390
                                nf <= res[BPBX2M1];
3391
                                zf <= res[`DBLBYTE]==24'h0000;
3392
                                acca <= res[`HIBYTE];
3393
                                accb <= res[`LOBYTE];
3394
                        end
3395
                `OIM_DP,`OIM_NDX,`OIM_EXT,
3396
                `EIM_DP,`EIM_NDX,`EIM_EXT,
3397
                `TIM_DP,`TIM_NDX,`TIM_EXT,
3398
                `AIM_DP,`AIM_NDX,`AIM_EXT:
3399
                        begin
3400
                                vf <= 1'b0;
3401
                                nf <= res12n;
3402
                                zf <= res12z;
3403
                        end
3404
`endif
3405 2 robfinch
                `ANDA_IMM,`ANDA_DP,`ANDA_NDX,`ANDA_EXT:
3406
                        begin
3407
                                nf <= res12n;
3408
                                zf <= res12z;
3409
                                vf <= 1'b0;
3410
                                acca <= res12[`LOBYTE];
3411
                        end
3412
                `ANDB_IMM,`ANDB_DP,`ANDB_NDX,`ANDB_EXT:
3413
                        begin
3414
                                nf <= res12n;
3415
                                zf <= res12z;
3416
                                vf <= 1'b0;
3417
                                accb <= res12[`LOBYTE];
3418
                        end
3419 21 robfinch
`ifdef SUPPORT_6309
3420
                `ORD_IMM,`ORD_DP,`ORD_NDX,`ORD_EXT,
3421
                `EORD_IMM,`EORD_DP,`EORD_NDX,`EORD_EXT,
3422
                `ANDD_IMM,`ANDD_DP,`ANDD_NDX,`ANDD_EXT:
3423
                        begin
3424
                                nf <= res24n;
3425
                                zf <= res24z;
3426
                                vf <= 1'b0;
3427
                                acca <= res[`HIBYTE];
3428
                                accb <= res[`LOBYTE];
3429
                        end
3430
                `BITD_IMM,`BITD_DP,`BITD_NDX,`BITD_EXT:
3431
                        begin
3432
                                nf <= res24n;
3433
                                zf <= res24z;
3434
                                vf <= 1'b0;
3435
                        end
3436 22 robfinch
                `DIVD_IMM,`DIVD_DP,`DIVD_NDX,`DIVD_EXT:
3437
                        begin
3438
                                acca <= res[`BYTE2];
3439
                                accb <= res[`BYTE1];
3440
                                // Overflow set eariler
3441
                                cf <= res[0];
3442
                                nf <= res[bitsPerByte-1];
3443
                                zf <= ~|res[bitsPerByte-1:0];
3444
                        end
3445
                `DIVQ_IMM,`DIVQ_DP,`DIVQ_NDX,`DIVQ_EXT:
3446
                        begin
3447
                                if (bitsPerByte==12) begin
3448
                                        acce <= divrem24[`BYTE2];
3449
                                        accf <= divrem24[`BYTE1];
3450
                                        acca <= divres48[`BYTE2];
3451
                                        accb <= divres48[`BYTE1];
3452
                                        // Overflow set eariler
3453
                                        cf <= divres48[0];
3454
                                        vf <= divres48[47:24]!={24{divres48[23]}};
3455
                                        nf <= divres48[23];
3456
                                        zf <= ~|divres48[23:0];
3457
                                end
3458
                                else if (bitsPerByte==8) begin
3459
                                        acce <= divrem16[`BYTE2];
3460
                                        accf <= divrem16[`BYTE1];
3461
                                        acca <= divres32[`BYTE2];
3462
                                        accb <= divres32[`BYTE1];
3463
                                        // Overflow set eariler
3464
                                        cf <= divres32[0];
3465
                                        vf <= divres32[31:16]!={16{divres32[15]}};
3466
                                        nf <= divres48[15];
3467
                                        zf <= ~|divres48[15:0];
3468
                                end
3469
                        end
3470
                `MULD_IMM,`MULD_DP,`MULD_NDX,`MULD_EXT:
3471
                        begin
3472
                                accb <= muld_res6[`BYTE1];
3473
                                acca <= muld_res6[`BYTE2];
3474
                                accf <= muld_res6[`BYTE3];
3475
                                acce <= muld_res6[`BYTE4];
3476
                                zf <= ~|muld_res6;
3477
                                nf <= muld_res6[bitsPerByte*4-1];
3478
                        end
3479 21 robfinch
`endif
3480 2 robfinch
                `ASLA:
3481
                        begin
3482
                                cf <= res12c;
3483
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3484
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3485
                                nf <= res12[BPBM1];
3486
                                zf <= res12[`LOBYTE]==12'h000;
3487
                                acca <= res12[`LOBYTE];
3488
                        end
3489
                `ASLB:
3490
                        begin
3491
                                cf <= res12c;
3492
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3493
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3494
                                nf <= res12[BPBM1];
3495
                                zf <= res12[`LOBYTE]==12'h000;
3496
                                accb <= res12[`LOBYTE];
3497
                        end
3498 21 robfinch
`ifdef H6309
3499
                `ASLD,`ROLD:
3500
                        begin
3501
                                cf <= resc;
3502
                                nf <= resn;
3503
                                zf <= resz;
3504
                                vf <= acca[bitsPerByte-1]^acca[bitsPerByte-2];
3505
                                acca <= res[`HIBYTE];
3506
                                accb <= res[`LOBYTE];
3507
                        end
3508
                `ASRD:
3509
                        begin
3510
                                cf <= resc;
3511
                                nf <= resn;
3512
                                zf <= resz;
3513
                                vf <= acca[bitsPerByte-1]^acca[bitsPerByte-2];
3514
                                acca <= res[`HIBYTE];
3515
                                accb <= res[`LOBYTE];
3516
                        end
3517
                `LSRD,`RORD:
3518
                        begin
3519
                                cf <= resc;
3520
                                nf <= resn;
3521
                                zf <= resz;
3522
                                vf <= acca[bitsPerByte-1]^acca[bitsPerByte-2];
3523
                                acca <= res[`HIBYTE];
3524
                                accb <= res[`LOBYTE];
3525
                        end
3526
                `LSRW:
3527
                        begin
3528
                                cf <= resc;
3529
                                nf <= resn;
3530
                                zf <= resz;
3531
                                vf <= acce[bitsPerByte-1]^acce[bitsPerByte-2];
3532
                                acce <= res[`HIBYTE];
3533
                                accf <= res[`LOBYTE];
3534
                        end
3535
                `ROLW,`RORW:
3536
                        begin
3537
                                cf <= resc;
3538
                                nf <= resn;
3539
                                zf <= resz;
3540
                                vf <= acce[bitsPerByte-1]^acce[bitsPerByte-2];
3541
                                acce <= res[`HIBYTE];
3542
                                accf <= res[`LOBYTE];
3543
                        end
3544
`endif
3545 2 robfinch
                `ASL_DP,`ASL_NDX,`ASL_EXT:
3546
                        begin
3547
                                cf <= res12c;
3548
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3549
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
3550
                                nf <= res12[BPBM1];
3551
                                zf <= res12[`LOBYTE]==12'h000;
3552
                        end
3553
                `ASRA:
3554
                        begin
3555
                                cf <= res12c;
3556
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3557
                                nf <= res12[BPBM1];
3558
                                zf <= res12[`LOBYTE]==12'h000;
3559
                                acca <= res12[`LOBYTE];
3560
                        end
3561
                `ASRB:
3562
                        begin
3563
                                cf <= res12c;
3564
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3565
                                nf <= res12[BPBM1];
3566
                                zf <= res12[`LOBYTE]==12'h000;
3567
                                accb <= res12[`LOBYTE];
3568
                        end
3569
                `ASR_DP,`ASR_NDX,`ASR_EXT:
3570
                        begin
3571
                                cf <= res12c;
3572
                                hf <= (a[`HCBIT]&b[`HCBIT])|(a[`HCBIT]&~res12[`HCBIT])|(b[`HCBIT]&~res12[`HCBIT]);
3573
                                nf <= res12[BPBM1];
3574
                                zf <= res12[`LOBYTE]==12'h000;
3575
                        end
3576
                `BITA_IMM,`BITA_DP,`BITA_NDX,`BITA_EXT,
3577
                `BITB_IMM,`BITB_DP,`BITB_NDX,`BITB_EXT:
3578
                        begin
3579
                                vf <= 1'b0;
3580
                                nf <= res12[BPBM1];
3581
                                zf <= res12[`LOBYTE]==12'h000;
3582
                        end
3583
                `CLRA:
3584
                        begin
3585
                                vf <= 1'b0;
3586
                                cf <= 1'b0;
3587
                                nf <= 1'b0;
3588
                                zf <= 1'b1;
3589
                                acca <= 12'h000;
3590
                        end
3591
                `CLRB:
3592
                        begin
3593
                                vf <= 1'b0;
3594
                                cf <= 1'b0;
3595
                                nf <= 1'b0;
3596
                                zf <= 1'b1;
3597
                                accb <= 12'h000;
3598
                        end
3599 21 robfinch
`ifdef SUPPORT_6309
3600 22 robfinch
                `BITMD:
3601
                        zf <= res12==12'h0;
3602 21 robfinch
                `CLRD:
3603
                        begin
3604
                                vf <= 1'b0;
3605
                                cf <= 1'b0;
3606
                                nf <= 1'b0;
3607
                                zf <= 1'b1;
3608
                                acca <= 12'h000;
3609
                                accb <= 12'h000;
3610
                        end
3611
                `CLRW:
3612
                        begin
3613
                                vf <= 1'b0;
3614
                                cf <= 1'b0;
3615
                                nf <= 1'b0;
3616
                                zf <= 1'b1;
3617
                                acce <= 12'h000;
3618
                                accf <= 12'h000;
3619
                        end
3620
                `CLRE:
3621
                        begin
3622
                                vf <= 1'b0;
3623
                                cf <= 1'b0;
3624
                                nf <= 1'b0;
3625
                                zf <= 1'b1;
3626
                                acce <= 12'h000;
3627
                        end
3628
                `CLRF:
3629
                        begin
3630
                                vf <= 1'b0;
3631
                                cf <= 1'b0;
3632
                                nf <= 1'b0;
3633
                                zf <= 1'b1;
3634
                                accf <= 12'h000;
3635
                        end
3636
`endif
3637 2 robfinch
                `CLR_DP,`CLR_NDX,`CLR_EXT:
3638
                        begin
3639
                                vf <= 1'b0;
3640
                                cf <= 1'b0;
3641
                                nf <= 1'b0;
3642
                                zf <= 1'b1;
3643
                        end
3644 21 robfinch
`ifdef SUPPORT_6309
3645
                `CMPE_IMM,`CMPE_DP,`CMPE_NDX,`CMPE_EXT,
3646 22 robfinch
                `CMPF_IMM,`CMPF_DP,`CMPF_NDX,`CMPF_EXT:
3647
                        begin
3648
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3649
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3650
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3651
                                nf <= res12[BPBM1];
3652
                                zf <= res12[`LOBYTE]==12'h000;
3653
                        end
3654 21 robfinch
`endif
3655 2 robfinch
                `CMPA_IMM,`CMPA_DP,`CMPA_NDX,`CMPA_EXT,
3656
                `CMPB_IMM,`CMPB_DP,`CMPB_NDX,`CMPB_EXT:
3657
                        begin
3658 22 robfinch
                                cf <= dm ? bcdsubbcf : (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
3659 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
3660
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
3661
                                nf <= res12[BPBM1];
3662
                                zf <= res12[`LOBYTE]==12'h000;
3663
                        end
3664 21 robfinch
`ifdef SUPPORT_6309
3665 22 robfinch
                `CMPW_IMM,`CMPW_DP,`CMPW_NDX,`CMPW_EXT:
3666
                        begin
3667
                                cf <= (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
3668
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3669
                                nf <= res[BPBX2M1];
3670
                                zf <= res[`DBLBYTE]==24'h000000;
3671
                        end
3672 21 robfinch
`endif
3673 2 robfinch
                `CMPD_IMM,`CMPD_DP,`CMPD_NDX,`CMPD_EXT:
3674
                        begin
3675 22 robfinch
                                cf <= dm ? bcdsubcf : (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
3676 2 robfinch
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3677
                                nf <= res[BPBX2M1];
3678
                                zf <= res[`DBLBYTE]==24'h000000;
3679
                        end
3680
                `CMPS_IMM,`CMPS_DP,`CMPS_NDX,`CMPS_EXT,
3681
                `CMPU_IMM,`CMPU_DP,`CMPU_NDX,`CMPU_EXT,
3682
                `CMPX_IMM,`CMPX_DP,`CMPX_NDX,`CMPX_EXT,
3683
                `CMPY_IMM,`CMPY_DP,`CMPY_NDX,`CMPY_EXT:
3684
                        begin
3685
                                cf <= (~a[BPBX2M1]&b[BPBX2M1])|(res[BPBX2M1]&~a[BPBX2M1])|(res[BPBX2M1]&b[BPBX2M1]);
3686
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
3687
                                nf <= res[BPBX2M1];
3688
                                zf <= res[`DBLBYTE]==24'h000000;
3689
                        end
3690
                `COMA:
3691
                        begin
3692
                                cf <= 1'b1;
3693
                                vf <= 1'b0;
3694
                                nf <= res12n;
3695
                                zf <= res12z;
3696
                                acca <= res12[`LOBYTE];
3697
                        end
3698
                `COMB:
3699
                        begin
3700
                                cf <= 1'b1;
3701
                                vf <= 1'b0;
3702
                                nf <= res12n;
3703
                                zf <= res12z;
3704
                                accb <= res12[`LOBYTE];
3705
                        end
3706 21 robfinch
`ifdef SUPPORT_6309
3707
                `COME:
3708
                        begin
3709
                                cf <= 1'b1;
3710
                                vf <= 1'b0;
3711
                                nf <= res12n;
3712
                                zf <= res12z;
3713
                                acce <= res12[`LOBYTE];
3714
                        end
3715
                `COMF:
3716
                        begin
3717
                                cf <= 1'b1;
3718
                                vf <= 1'b0;
3719
                                nf <= res12n;
3720
                                zf <= res12z;
3721
                                accf <= res12[`LOBYTE];
3722
                        end
3723
                `COMD:
3724
                        begin
3725
                                cf <= 1'b1;
3726
                                vf <= 1'b0;
3727
                                nf <= res24n;
3728
                                zf <= res24z;
3729
                                {acca,accb} <= res;
3730
                        end
3731
                `COMW:
3732
                        begin
3733
                                cf <= 1'b1;
3734
                                vf <= 1'b0;
3735
                                nf <= res24n;
3736
                                zf <= res24z;
3737
                                {acce,accf} <= res;
3738
                        end
3739
`endif
3740 2 robfinch
                `COM_DP,`COM_NDX,`COM_EXT:
3741
                        begin
3742
                                cf <= 1'b1;
3743
                                vf <= 1'b0;
3744
                                nf <= res12n;
3745
                                zf <= res12z;
3746
                        end
3747
                `DAA:
3748
                        begin
3749
                                cf <= res12c;
3750
                                zf <= res12z;
3751
                                nf <= res12n;
3752
                                vf <= (res12[BPBM1] ^ b[BPBM1]) & (1'b1 ^ a[BPBM1] ^ b[BPBM1]);
3753
                                acca <= res12[`LOBYTE];
3754
                        end
3755
                `DECA:
3756
                        begin
3757
                                nf <= res12n;
3758
                                zf <= res12z;
3759
                                vf <= res12[BPBM1] != acca[BPBM1];
3760
                                acca <= res12[`LOBYTE];
3761
                        end
3762
                `DECB:
3763
                        begin
3764
                                nf <= res12n;
3765
                                zf <= res12z;
3766
                                vf <= res12[BPBM1] != accb[BPBM1];
3767
                                accb <= res12[`LOBYTE];
3768
                        end
3769 21 robfinch
`ifdef SUPPORT_6309
3770
                `DECE:
3771
                        begin
3772
                                nf <= res12n;
3773
                                zf <= res12z;
3774
                                vf <= res12[BPBM1] != acce[BPBM1];
3775
                                acce <= res12[`LOBYTE];
3776
                        end
3777
                `DECF:
3778
                        begin
3779
                                nf <= res12n;
3780
                                zf <= res12z;
3781
                                vf <= res12[BPBM1] != accf[BPBM1];
3782
                                accf <= res12[`LOBYTE];
3783
                        end
3784
                `DECD:
3785
                        begin
3786
                                nf <= res24n;
3787
                                zf <= res24z;
3788
                                vf <= res[bitsPerByte*2-1] != acca[bitsPerByte-1];
3789
                                {acca,accb} <= res;
3790
                        end
3791
                `DECW:
3792
                        begin
3793
                                nf <= res24n;
3794
                                zf <= res24z;
3795
                                vf <= res[bitsPerByte*2-1] != acce[bitsPerByte-1];
3796
                                {acce,accf} <= res;
3797
                        end
3798
`endif
3799 2 robfinch
                `DEC_DP,`DEC_NDX,`DEC_EXT:
3800
                        begin
3801
                                nf <= res12n;
3802
                                zf <= res12z;
3803
                                vf <= res12[BPBM1] != b[BPBM1];
3804
                        end
3805
                `EORA_IMM,`EORA_DP,`EORA_NDX,`EORA_EXT,
3806
                `ORA_IMM,`ORA_DP,`ORA_NDX,`ORA_EXT:
3807
                        begin
3808
                                nf <= res12n;
3809
                                zf <= res12z;
3810
                                vf <= 1'b0;
3811
                                acca <= res12[`LOBYTE];
3812
                        end
3813
                `EORB_IMM,`EORB_DP,`EORB_NDX,`EORB_EXT,
3814
                `ORB_IMM,`ORB_DP,`ORB_NDX,`ORB_EXT:
3815
                        begin
3816
                                nf <= res12n;
3817
                                zf <= res12z;
3818
                                vf <= 1'b0;
3819
                                accb <= res12[`LOBYTE];
3820
                        end
3821
                `EXG:
3822
                        begin
3823
                                case(ir[bitsPerByte+3:bitsPerByte])
3824
                                4'b0000:
3825
                                                        begin
3826
                                                                acca <= src1[`HIBYTE];
3827
                                                                accb <= src1[`LOBYTE];
3828
                                                        end
3829
                                4'b0001:        xr <= src1;
3830
                                4'b0010:        yr <= src1;
3831
                                4'b0011:        usp <= src1;
3832
                                4'b0100:        begin ssp <= src1; nmi_armed <= `TRUE; end
3833
                                4'b0101:        pc <= src1[`DBLBYTE];
3834
                                4'b1000:        acca <= src1[`LOBYTE];
3835
                                4'b1001:        accb <= src1[`LOBYTE];
3836
                                4'b1010:
3837
                                        begin
3838
                                                cf <= src1[0];
3839
                                                vf <= src1[1];
3840
                                                zf <= src1[2];
3841
                                                nf <= src1[3];
3842
                                                im <= src1[4];
3843
                                                hf <= src1[5];
3844
                                                firqim <= src1[6];
3845
                                                ef <= src1[7];
3846 22 robfinch
                                                dm <= src1[8];
3847 2 robfinch
                                        end
3848
                                4'b1011:        dpr <= src1[`LOBYTE];
3849 21 robfinch
                                4'b1100:        usppg <= src1[`DBLBYTE];
3850
`ifdef SUPPORT_6309
3851
                                4'b0110:        {acce,accf} <= src1[`DBLBYTE];
3852
                                4'b1110:        acce <= src1[`LOBYTE];
3853
                                4'b1111:        accf <= src1[`LOBYTE];
3854
`else
3855
                                4'b1110:        ;
3856 2 robfinch
                                4'b1111:        ;
3857 21 robfinch
`endif
3858 2 robfinch
                                default:        ;
3859
                                endcase
3860
                                case(ir[bitsPerByte+7:bitsPerByte+4])
3861
                                4'b0000:
3862
                                                        begin
3863
                                                                acca <= src2[`HIBYTE];
3864
                                                                accb <= src2[`LOBYTE];
3865
                                                        end
3866
                                4'b0001:        xr <= src2;
3867
                                4'b0010:        yr <= src2;
3868
                                4'b0011:        usp <= src2;
3869
                                4'b0100:        begin ssp <= src2; nmi_armed <= `TRUE; end
3870
                                4'b0101:        pc <= src2[`DBLBYTE];
3871
                                4'b1000:        acca <= src2[`LOBYTE];
3872
                                4'b1001:        accb <= src2[`LOBYTE];
3873
                                4'b1010:
3874
                                        begin
3875
                                                cf <= src2[0];
3876
                                                vf <= src2[1];
3877
                                                zf <= src2[2];
3878
                                                nf <= src2[3];
3879
                                                im <= src2[4];
3880
                                                hf <= src2[5];
3881
                                                firqim <= src2[6];
3882
                                                ef <= src2[7];
3883 22 robfinch
                                                dm <= src2[8];
3884 2 robfinch
                                        end
3885
                                4'b1011:        dpr <= src2[`LOBYTE];
3886 21 robfinch
                                4'b1100:        usppg <= src2[`DBLBYTE];
3887
`ifdef SUPPORT_6309
3888
                                4'b1110:        acce <= src2[`LOBYTE];
3889
                                4'b1111:        accf <= src2[`LOBYTE];
3890
`else
3891
                                4'b1110:        ;
3892 2 robfinch
                                4'b1111:        ;
3893 21 robfinch
`endif
3894 2 robfinch
                                default:        ;
3895
                                endcase
3896
                        end
3897
                `INCA:
3898
                        begin
3899
                                nf <= res12n;
3900
                                zf <= res12z;
3901
                                vf <= res12[BPBM1] != acca[BPBM1];
3902
                                acca <= res12[`LOBYTE];
3903
                        end
3904
                `INCB:
3905
                        begin
3906
                                nf <= res12n;
3907
                                zf <= res12z;
3908
                                vf <= res12[BPBM1] != accb[BPBM1];
3909
                                accb <= res12[`LOBYTE];
3910
                        end
3911 21 robfinch
`ifdef SUPPORT_6309
3912
                `INCE:
3913
                        begin
3914
                                nf <= res12n;
3915
                                zf <= res12z;
3916
                                vf <= res12[BPBM1] != acce[BPBM1];
3917
                                acce <= res12[`LOBYTE];
3918
                        end
3919
                `INCF:
3920
                        begin
3921
                                nf <= res12n;
3922
                                zf <= res12z;
3923
                                vf <= res12[BPBM1] != accf[BPBM1];
3924
                                accf <= res12[`LOBYTE];
3925
                        end
3926
                `INCD:
3927
                        begin
3928
                                nf <= res24n;
3929
                                zf <= res24z;
3930
                                vf <= res[bitsPerByte*2-1] != acca[bitsPerByte-1];
3931
                                {acca,accb} <= res[`LOBYTE];
3932
                        end
3933
                `INCW:
3934
                        begin
3935
                                nf <= res24n;
3936
                                zf <= res24z;
3937
                                vf <= res[bitsPerByte*2-1] != acce[bitsPerByte-1];
3938
                                {acce,accf} <= res[`LOBYTE];
3939
                        end
3940
                `LDE_IMM,`LDE_DP,`LDE_NDX,`LDE_EXT:
3941
                        begin
3942
                                vf <= 1'b0;
3943
                                zf <= res12z;
3944
                                nf <= res12n;
3945
                                acce <= res12[`LOBYTE];
3946
                        end
3947
                `LDF_IMM,`LDF_DP,`LDF_NDX,`LDF_EXT:
3948
                        begin
3949
                                vf <= 1'b0;
3950
                                zf <= res12z;
3951
                                nf <= res12n;
3952
                                accf <= res12[`LOBYTE];
3953
                        end
3954
`endif
3955 2 robfinch
                `INC_DP,`INC_NDX,`INC_EXT:
3956
                        begin
3957
                                nf <= res12n;
3958
                                zf <= res12z;
3959
                                vf <= res12[BPBM1] != b[BPBM1];
3960
                        end
3961
                `LDA_IMM,`LDA_DP,`LDA_NDX,`LDA_EXT:
3962
                        begin
3963
                                vf <= 1'b0;
3964
                                zf <= res12z;
3965
                                nf <= res12n;
3966
                                acca <= res12[`LOBYTE];
3967
                        end
3968
                `LDB_IMM,`LDB_DP,`LDB_NDX,`LDB_EXT:
3969
                        begin
3970
                                vf <= 1'b0;
3971
                                zf <= res12z;
3972
                                nf <= res12n;
3973
                                accb <= res12[`LOBYTE];
3974
                        end
3975
                `LDD_IMM,`LDD_DP,`LDD_NDX,`LDD_EXT:
3976
                        begin
3977
                                vf <= 1'b0;
3978
                                zf <= res24z;
3979
                                nf <= res24n;
3980
                                acca <= res[`HIBYTE];
3981
                                accb <= res[`LOBYTE];
3982
                        end
3983 21 robfinch
`ifdef SUPPORT_6309
3984
                `LDW_IMM,`LDW_DP,`LDW_NDX,`LDW_EXT:
3985
                        begin
3986
                                vf <= 1'b0;
3987
                                zf <= res24z;
3988
                                nf <= res24n;
3989
                                acce <= res[`HIBYTE];
3990
                                accf <= res[`LOBYTE];
3991
                        end
3992
`endif
3993 2 robfinch
                `LDU_IMM,`LDU_DP,`LDU_NDX,`LDU_EXT:
3994
                        begin
3995
                                vf <= 1'b0;
3996
                                zf <= res24z;
3997
                                nf <= res24n;
3998
                                usp <= res[`DBLBYTE];
3999
                        end
4000
                `LDS_IMM,`LDS_DP,`LDS_NDX,`LDS_EXT:
4001
                        begin
4002
                                vf <= 1'b0;
4003
                                zf <= res24z;
4004
                                nf <= res24n;
4005
                                ssp <= res[`DBLBYTE];
4006
                                nmi_armed <= 1'b1;
4007
                        end
4008
                `LDX_IMM,`LDX_DP,`LDX_NDX,`LDX_EXT:
4009
                        begin
4010
                                vf <= 1'b0;
4011
                                zf <= res24z;
4012
                                nf <= res24n;
4013
                                xr <= res[`DBLBYTE];
4014
                        end
4015
                `LDY_IMM,`LDY_DP,`LDY_NDX,`LDY_EXT:
4016
                        begin
4017
                                vf <= 1'b0;
4018
                                zf <= res24z;
4019
                                nf <= res24n;
4020
                                yr <= res[`DBLBYTE];
4021
                        end
4022
                `LEAS_NDX:
4023
                        begin ssp <= res[`DBLBYTE]; nmi_armed <= 1'b1; end
4024
                `LEAU_NDX:
4025
                        usp <= res[`DBLBYTE];
4026
                `LEAX_NDX:
4027
                        begin
4028
                                zf <= res24z;
4029
                                xr <= res[`DBLBYTE];
4030
                        end
4031
                `LEAY_NDX:
4032
                        begin
4033
                                zf <= res24z;
4034
                                yr <= res[`DBLBYTE];
4035
                        end
4036
                `LSRA:
4037
                        begin
4038
                                cf <= res12c;
4039
                                nf <= res12[BPBM1];
4040
                                zf <= res12[`LOBYTE]==12'h000;
4041
                                acca <= res12[`LOBYTE];
4042
                        end
4043
                `LSRB:
4044
                        begin
4045
                                cf <= res12c;
4046
                                nf <= res12[BPBM1];
4047
                                zf <= res12[`LOBYTE]==12'h000;
4048
                                accb <= res12[`LOBYTE];
4049
                        end
4050
                `LSR_DP,`LSR_NDX,`LSR_EXT:
4051
                        begin
4052
                                cf <= res12c;
4053
                                nf <= res12[BPBM1];
4054
                                zf <= res12[`LOBYTE]==12'h000;
4055
                        end
4056
                `MUL:
4057 22 robfinch
                        if (dm) begin
4058
                                accb <= bcdmul_res16[`BYTE1];
4059
                                acca <= bcdmul_res16[`BYTE2];
4060
                                zf <= ~|bcdmul_res16;
4061
                                cf <= bcdmul_res16[bitsPerByte*2-1];
4062
                        end
4063
                        else begin
4064 2 robfinch
                                cf <= prod[BPBM1];
4065
                                zf <= res24z;
4066
                                acca <= prod[`HIBYTE];
4067
                                accb <= prod[`LOBYTE];
4068
                        end
4069
                `NEGA:
4070
                        begin
4071 22 robfinch
                                cf <= dm ? bcdnegbcf : (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
4072 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4073
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4074
                                nf <= res12[BPBM1];
4075
                                zf <= res12[`LOBYTE]==12'h000;
4076
                                acca <= res12[`LOBYTE];
4077
                        end
4078
                `NEGB:
4079
                        begin
4080 22 robfinch
                                cf <= dm ? bcdnegbcf : (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
4081 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4082
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4083
                                nf <= res12[BPBM1];
4084
                                zf <= res12[`LOBYTE]==12'h000;
4085
                                accb <= res12[`LOBYTE];
4086
                        end
4087 21 robfinch
`ifdef SUPPORT_6309
4088
                `NEGD:
4089
                        begin
4090 22 robfinch
                                cf <= dm ? bcdnegcf : (~a[bitsPerByte*2-1]&b[bitsPerByte*2-1])|(res[bitsPerByte*2-1]&~a[bitsPerByte*2-1])|(res[bitsPerByte*2-1]&b[bitsPerByte*2-1]);
4091 21 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res[`HCBIT]&~a[`HCBIT])|(res[`HCBIT]&b[`HCBIT]);
4092
                                vf <= (1'b1 ^ res[bitsPerByte*2-1] ^ b[bitsPerByte*2-1]) & (a[bitsPerByte*2-1] ^ b[bitsPerByte*2-1]);
4093
                                nf <= res[bitsPerByte*2-1];
4094
                                zf <= res[`DBLBYTE]=='h0;
4095
                                {acca,accb} <= res;
4096
                        end
4097
`endif
4098 2 robfinch
                `NEG_DP,`NEG_NDX,`NEG_EXT:
4099
                        begin
4100
                                cf <= (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
4101
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4102
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4103
                                nf <= res12[BPBM1];
4104
                                zf <= res12[`LOBYTE]==12'h000;
4105
                        end
4106
                `ROLA:
4107
                        begin
4108
                                cf <= res12c;
4109
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
4110
                                nf <= res12[BPBM1];
4111
                                zf <= res12[`LOBYTE]==12'h000;
4112
                                acca <= res12[`LOBYTE];
4113
                        end
4114
                `ROLB:
4115
                        begin
4116
                                cf <= res12c;
4117
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
4118
                                nf <= res12[BPBM1];
4119
                                zf <= res12[`LOBYTE]==12'h000;
4120
                                accb <= res12[`LOBYTE];
4121
                        end
4122
                `ROL_DP,`ROL_NDX,`ROL_EXT:
4123
                        begin
4124
                                cf <= res12c;
4125
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
4126
                                nf <= res12[BPBM1];
4127
                                zf <= res12[`LOBYTE]==12'h000;
4128
                        end
4129
                `RORA:
4130
                        begin
4131
                                cf <= res12c;
4132
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
4133
                                nf <= res12[BPBM1];
4134
                                zf <= res12[`LOBYTE]==12'h000;
4135
                                acca <= res12[`LOBYTE];
4136
                        end
4137
                `RORB:
4138
                        begin
4139
                                cf <= res12c;
4140
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
4141
                                nf <= res12[BPBM1];
4142
                                zf <= res12[`LOBYTE]==12'h000;
4143
                                accb <= res12[`LOBYTE];
4144
                        end
4145
                `ROR_DP,`ROR_NDX,`ROR_EXT:
4146
                        begin
4147
                                cf <= res12c;
4148
                                vf <= res12[BPBM1] ^ res12[bitsPerByte];
4149
                                nf <= res12[BPBM1];
4150
                                zf <= res12[`LOBYTE]==12'h000;
4151
                        end
4152
                `SBCA_IMM,`SBCA_DP,`SBCA_NDX,`SBCA_EXT:
4153
                        begin
4154 22 robfinch
                                cf <= dm ? bcdsubbcf : (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
4155 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4156
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4157
                                nf <= res12[BPBM1];
4158
                                zf <= res12[`LOBYTE]==12'h000;
4159
                                acca <= res12[`LOBYTE];
4160
                        end
4161
                `SBCB_IMM,`SBCB_DP,`SBCB_NDX,`SBCB_EXT:
4162
                        begin
4163 22 robfinch
                                cf <= dm ? bcdsubbcf : (~a[BPBM1]&b[BPBM1])|(res12[BPBM1]&~a[BPBM1])|(res12[BPBM1]&b[BPBM1]);
4164 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4165
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4166
                                nf <= res12[BPBM1];
4167
                                zf <= res12[`LOBYTE]==12'h000;
4168
                                accb <= res12[`LOBYTE];
4169
                        end
4170
                `SEX:
4171
                        begin
4172
                                vf <= 1'b0;
4173
                                nf <= res12n;
4174
                                zf <= res12z;
4175
                                acca <= res12[`LOBYTE];
4176
                        end
4177 21 robfinch
`ifdef SUPPORT_6309
4178
                `STE_DP,`STE_NDX,`STE_EXT,
4179
                `STF_DP,`STF_NDX,`STF_EXT,
4180
`endif
4181 2 robfinch
                `STA_DP,`STA_NDX,`STA_EXT,
4182
                `STB_DP,`STB_NDX,`STB_EXT:
4183
                        begin
4184
                                vf <= 1'b0;
4185
                                zf <= res12z;
4186
                                nf <= res12n;
4187
                        end
4188 21 robfinch
`ifdef SUPPORT_6309
4189
                `STW_DP,`STW_NDX,`STW_EXT,
4190
`endif
4191 2 robfinch
                `STD_DP,`STD_NDX,`STD_EXT,
4192
                `STU_DP,`STU_NDX,`STU_EXT,
4193
                `STX_DP,`STX_NDX,`STX_EXT,
4194
                `STY_DP,`STY_NDX,`STY_EXT:
4195
                        begin
4196
                                vf <= 1'b0;
4197
                                zf <= res24z;
4198
                                nf <= res24n;
4199
                        end
4200
                `TFR:
4201
                        begin
4202
                                case(ir[bitsPerByte+3:bitsPerByte])
4203
                                4'b0000:
4204
                                                        begin
4205
                                                                acca <= src1[`HIBYTE];
4206
                                                                accb <= src1[`LOBYTE];
4207
                                                        end
4208
                                4'b0001:        xr <= src1;
4209
                                4'b0010:        yr <= src1;
4210
                                4'b0011:        usp <= src1;
4211
                                4'b0100:        begin ssp <= src1; nmi_armed <= `TRUE; end
4212
                                4'b0101:        pc <= src1[`DBLBYTE];
4213
                                4'b1000:        acca <= src1[`LOBYTE];
4214
                                4'b1001:        accb <= src1[`LOBYTE];
4215
                                4'b1010:
4216
                                        begin
4217
                                                cf <= src1[0];
4218
                                                vf <= src1[1];
4219
                                                zf <= src1[2];
4220
                                                nf <= src1[3];
4221
                                                im <= src1[4];
4222
                                                hf <= src1[5];
4223
                                                firqim <= src1[6];
4224
                                                ef <= src1[7];
4225 22 robfinch
                                                dm <= src1[8];
4226 2 robfinch
                                        end
4227
                                4'b1011:        dpr <= src1[`LOBYTE];
4228 21 robfinch
                                4'b1100:        usppg <= src1[`DBLBYTE];
4229
`ifdef SUPPORT_6309
4230
                                4'b0110:        {acce,accf} <= src1[`DBLBYTE];
4231
                                4'b1110:        acce <= src1[`LOBYTE];
4232
                                4'b1111:        accf <= src1[`LOBYTE];
4233
`else
4234
                                4'b1110:        ;
4235 2 robfinch
                                4'b1111:        ;
4236 21 robfinch
`endif
4237 2 robfinch
                                default:        ;
4238
                                endcase
4239
                        end
4240 21 robfinch
                `TSTE,`TSTF,
4241 2 robfinch
                `TSTA,`TSTB:
4242
                        begin
4243
                                vf <= 1'b0;
4244
                                nf <= res12n;
4245
                                zf <= res12z;
4246
                        end
4247
                `TSTD:
4248
                        begin
4249
                                vf <= 1'b0;
4250
                                nf <= res24n;
4251
                                zf <= res24z;
4252
                        end
4253 21 robfinch
`ifdef SUPPORT_6309
4254
                `TSTW:
4255
                        begin
4256
                                vf <= 1'b0;
4257
                                nf <= res24n;
4258
                                zf <= res24z;
4259
                        end
4260
`endif
4261 2 robfinch
                `TST_DP,`TST_NDX,`TST_EXT:
4262
                        begin
4263
                                vf <= 1'b0;
4264
                                nf <= res12n;
4265
                                zf <= res12z;
4266
                        end
4267
                `SUBA_IMM,`SUBA_DP,`SUBA_NDX,`SUBA_EXT:
4268
                        begin
4269
                                acca <= res12[`LOBYTE];
4270
                                nf <= res12n;
4271
                                zf <= res12z;
4272
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4273 22 robfinch
                                cf <= dm ? bcdsubbcf : res12c;
4274 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4275
                        end
4276
                `SUBB_IMM,`SUBB_DP,`SUBB_NDX,`SUBB_EXT:
4277
                        begin
4278
                                accb <= res12[`LOBYTE];
4279
                                nf <= res12n;
4280
                                zf <= res12z;
4281
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4282 22 robfinch
                                cf <= dm ? bcdsubbcf : res12c;
4283 2 robfinch
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4284
                        end
4285 21 robfinch
`ifdef SUPPORT_6309
4286
                `SUBE_IMM,`SUBE_DP,`SUBE_NDX,`SUBE_EXT:
4287
                        begin
4288
                                acce <= res12[`LOBYTE];
4289
                                nf <= res12n;
4290
                                zf <= res12z;
4291
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4292
                                cf <= res12c;
4293
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4294
                        end
4295
                `SUBF_IMM,`SUBF_DP,`SUBF_NDX,`SUBF_EXT:
4296
                        begin
4297
                                accf <= res12[`LOBYTE];
4298
                                nf <= res12n;
4299
                                zf <= res12z;
4300
                                vf <= (1'b1 ^ res12[BPBM1] ^ b[BPBM1]) & (a[BPBM1] ^ b[BPBM1]);
4301
                                cf <= res12c;
4302
                                hf <= (~a[`HCBIT]&b[`HCBIT])|(res12[`HCBIT]&~a[`HCBIT])|(res12[`HCBIT]&b[`HCBIT]);
4303
                        end
4304
                `SUBW_IMM,`SUBW_DP,`SUBW_NDX,`SUBW_EXT:
4305
                        begin
4306
                                cf <= res24c;
4307
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
4308
                                nf <= res[BPBX2M1];
4309
                                zf <= res[`DBLBYTE]==24'h000000;
4310
                                acce <= res[`HIBYTE];
4311
                                accf <= res[`LOBYTE];
4312
                        end
4313
                `SBCD_IMM,`SBCD_DP,`SBCD_NDX,`SBCD_EXT,
4314
`endif
4315 2 robfinch
                `SUBD_IMM,`SUBD_DP,`SUBD_NDX,`SUBD_EXT:
4316
                        begin
4317 22 robfinch
                                cf <= dm ? bcdsubcf : res24c;
4318 2 robfinch
                                vf <= (1'b1 ^ res[BPBX2M1] ^ b[BPBX2M1]) & (a[BPBX2M1] ^ b[BPBX2M1]);
4319
                                nf <= res[BPBX2M1];
4320
                                zf <= res[`DBLBYTE]==24'h000000;
4321
                                acca <= res[`HIBYTE];
4322
                                accb <= res[`LOBYTE];
4323
                        end
4324
                endcase
4325
        end
4326
end
4327
endtask
4328
 
4329
task dp_store;
4330
input [5:0] stw;
4331
begin
4332
        store_what <= stw;
4333
        wadr <= dp_address;
4334
        pc <= pc + 2'd2;
4335
        next_state(STORE1);
4336
end
4337
endtask
4338
 
4339
task indexed_store;
4340
input [5:0] stw;
4341
begin
4342
        store_what <= stw;
4343
        pc <= pc + insnsz;
4344
        if (isIndirect) begin
4345
                load_what <= isFar ? `LW_IA2316 : `LW_IAH;
4346
                radr <= NdxAddr;
4347
                next_state(LOAD1);
4348
        end
4349
        else begin
4350
                wadr <= NdxAddr;
4351
                next_state(STORE1);
4352
        end
4353
end
4354
endtask
4355
 
4356
task ex_store;
4357
input [5:0] stw;
4358
begin
4359
        pc <= pc + (isFar ? 3'd4 : 3'd3);
4360
        store_what <= stw;
4361
        wadr <= ex_address;
4362
        next_state(STORE1);
4363
end
4364
endtask
4365
 
4366
task next_state;
4367
input [5:0] st;
4368
begin
4369
        state <= st;
4370
end
4371
endtask
4372
 
4373
task wb_burst;
4374
input [5:0] len;
4375
input [bitsPerByte*2-1:0] adr;
4376
begin
4377
        if (!tsc) begin
4378
                bte_o <= 2'b00;
4379
                cti_o <= 3'b001;
4380
                bl_o <= len;
4381
                cyc_o <= 1'b1;
4382
                stb_o <= 1'b1;
4383 12 robfinch
                we_o <= 1'b0;
4384 2 robfinch
                adr_o <= adr;
4385
        end
4386
end
4387
endtask
4388
 
4389
task wb_read;
4390
input [`TRPBYTE] adr;
4391
begin
4392
        if (!tsc) begin
4393
                cyc_o <= 1'b1;
4394
                stb_o <= 1'b1;
4395 12 robfinch
                we_o <= 1'b0;
4396 2 robfinch
                adr_o <= adr;
4397
        end
4398
end
4399
endtask
4400
 
4401 22 robfinch
// Trap writes to special registers
4402 2 robfinch
task wb_write;
4403
input [`TRPBYTE] adr;
4404
input [`LOBYTE] dat;
4405
begin
4406 22 robfinch
                if (!tsc) begin
4407
                        next_state(IFETCH);
4408
`ifdef SUPPORT_DEBUG_REG
4409
                        case(adr)
4410
                        `BRKAD0+0:      brkad[0][`BYTE2] <= dat;
4411
                        `BRKAD0+1: brkad[0][`BYTE1] <= dat;
4412
                        `BRKAD1+0:      brkad[1][`BYTE2] <= dat;
4413
                        `BRKAD1+1: brkad[1][`BYTE1] <= dat;
4414
                        `BRKAD2+0:      brkad[2][`BYTE2] <= dat;
4415
                        `BRKAD2+1: brkad[2][`BYTE1] <= dat;
4416
                        `BRKAD3+0:      brkad[3][`BYTE2] <= dat;
4417
                        `BRKAD3+1: brkad[3][`BYTE1] <= dat;
4418
                        `BRKCTRL0: brkctrl[0] <= dat;
4419
                        `BRKCTRL1: brkctrl[1] <= dat;
4420
                        `BRKCTRL2: brkctrl[2] <= dat;
4421
                        `BRKCTRL3: brkctrl[3] <= dat;
4422
                        default:
4423
                                begin
4424
`endif
4425
                                we_o <= 1'b1;
4426
                                adr_o <= adr;
4427
                                dat_o <= dat;
4428
                                next_state(STORE1a);
4429
`ifdef SUPPORT_DEBUG_REG
4430
                                end
4431
                        endcase
4432
`endif
4433
                end
4434 2 robfinch
end
4435
endtask
4436
 
4437
task wb_nack;
4438
begin
4439
        cti_o <= 3'b000;
4440
        bl_o <= 6'd0;
4441
        cyc_o <= 1'b0;
4442
        stb_o <= 1'b0;
4443
        we_o <= 1'b0;
4444
        adr_o <= 24'd0;
4445
        dat_o <= 12'd0;
4446
end
4447
endtask
4448
 
4449
task load_tsk;
4450
input [`LOBYTE] dat;
4451
begin
4452
        case(load_what)
4453
        `LW_BH:
4454
                        begin
4455
                                radr <= radr + 2'd1;
4456
                                b[`HIBYTE] <= dat;
4457
                                load_what <= `LW_BL;
4458
                                next_state(LOAD1);
4459
                        end
4460
        `LW_BL:
4461
                        begin
4462
                                // Don't increment address here for the benefit of the memory
4463
                                // operate instructions which set wadr=radr in CALC.
4464
                                b[`LOBYTE] <= dat;
4465
                                next_state(CALC);
4466
                        end
4467
        `LW_CCR:        begin
4468
                                next_state(PULL1);
4469
                                radr <= radr + 2'd1;
4470
                                cf <= dat[0];
4471
                                vf <= dat[1];
4472
                                zf <= dat[2];
4473
                                nf <= dat[3];
4474
                                im <= dat[4];
4475
                                hf <= dat[5];
4476
                                firqim <= dat[6];
4477
                                ef <= dat[7];
4478 22 robfinch
                                dm <= dat[8];
4479 2 robfinch
                                if (isRTI) begin
4480
                                        $display("loaded ccr=%b", dat);
4481 22 robfinch
                                        ir[`HIBYTE] <= dat[7] ? 12'h3FE : 12'h080;
4482 2 robfinch
                                        ssp <= ssp + 2'd1;
4483
                                end
4484
                                else if (isPULS)
4485
                                        ssp <= ssp + 2'd1;
4486
                                else if (isPULU)
4487
                                        usp <= usp + 2'd1;
4488
                        end
4489
        `LW_ACCA:       begin
4490
                                acca <= dat;
4491
                                radr <= radr + 2'd1;
4492
                                if (isRTI) begin
4493
                                        $display("loaded acca=%h from %h", dat, radr);
4494
                                        ssp <= ssp + 2'd1;
4495
                                        next_state(PULL1);
4496
                                end
4497
                                else if (isPULU) begin
4498
                                        usp <= usp + 2'd1;
4499
                                        next_state(PULL1);
4500
                                end
4501
                                else if (isPULS) begin
4502
                                        ssp <= ssp + 2'd1;
4503
                                        next_state(PULL1);
4504
                                end
4505
                                else
4506
                                        next_state(IFETCH);
4507
                        end
4508
        `LW_ACCB:       begin
4509
                                accb <= dat;
4510
                                radr <= radr + 2'd1;
4511
                                if (isRTI) begin
4512 11 robfinch
                                        $display("loaded accb=%h from %h", dat, radr);
4513 2 robfinch
                                        ssp <= ssp + 2'd1;
4514
                                        next_state(PULL1);
4515
                                end
4516
                                else if (isPULU) begin
4517
                                        usp <= usp + 2'd1;
4518
                                        next_state(PULL1);
4519
                                end
4520
                                else if (isPULS) begin
4521
                                        ssp <= ssp + 2'd1;
4522
                                        next_state(PULL1);
4523
                                end
4524
                                else
4525
                                        next_state(IFETCH);
4526
                        end
4527 21 robfinch
`ifdef SUPPORT_6309
4528
        `LW_ACCE:       begin
4529
                                acce <= dat;
4530
                                radr <= radr + 2'd1;
4531
                                if (isRTI) begin
4532
                                        $display("loaded acce=%h from %h", dat, radr);
4533
                                        ssp <= ssp + 2'd1;
4534
                                        next_state(PULL1);
4535
                                end
4536
                                else if (isPULU) begin
4537
                                        usp <= usp + 2'd1;
4538
                                        next_state(PULL1);
4539
                                end
4540
                                else if (isPULS) begin
4541
                                        ssp <= ssp + 2'd1;
4542
                                        next_state(PULL1);
4543
                                end
4544
                                else
4545
                                        next_state(IFETCH);
4546
                        end
4547
        `LW_ACCF:       begin
4548
                                accf <= dat;
4549
                                radr <= radr + 2'd1;
4550
                                if (isRTI) begin
4551
                                        $display("loaded accf=%h from %h", dat, radr);
4552
                                        ssp <= ssp + 2'd1;
4553
                                        next_state(PULL1);
4554
                                end
4555
                                else if (isPULU) begin
4556
                                        usp <= usp + 2'd1;
4557
                                        next_state(PULL1);
4558
                                end
4559
                                else if (isPULS) begin
4560
                                        ssp <= ssp + 2'd1;
4561
                                        next_state(PULL1);
4562
                                end
4563
                                else
4564
                                        next_state(IFETCH);
4565
                        end
4566
`endif
4567 2 robfinch
        `LW_DPR:        begin
4568
                                dpr <= dat;
4569
                                radr <= radr + 2'd1;
4570
                                if (isRTI) begin
4571
                                        $display("loaded dpr=%h from %h", dat, radr);
4572
                                        ssp <= ssp + 2'd1;
4573
                                        next_state(PULL1);
4574
                                end
4575
                                else if (isPULU) begin
4576
                                        usp <= usp + 2'd1;
4577
                                        next_state(PULL1);
4578
                                end
4579
                                else if (isPULS) begin
4580
                                        ssp <= ssp + 2'd1;
4581
                                        next_state(PULL1);
4582
                                end
4583
                                else
4584
                                        next_state(IFETCH);
4585
                        end
4586
        `LW_XH: begin
4587
                                load_what <= `LW_XL;
4588
                                next_state(LOAD1);
4589
                                xr[`HIBYTE] <= dat;
4590
                                radr <= radr + 2'd1;
4591
                                if (isRTI) begin
4592
                                        $display("loaded XH=%h from %h", dat, radr);
4593
                                        ssp <= ssp + 2'd1;
4594
                                end
4595
                                else if (isPULU) begin
4596
                                        usp <= usp + 2'd1;
4597
                                end
4598
                                else if (isPULS) begin
4599
                                        ssp <= ssp + 2'd1;
4600
                                end
4601
                        end
4602
        `LW_XL: begin
4603
                                xr[`LOBYTE] <= dat;
4604
                                radr <= radr + 2'd1;
4605
                                if (isRTI) begin
4606
                                        $display("loaded XL=%h from %h", dat, radr);
4607
                                        ssp <= ssp + 2'd1;
4608
                                        next_state(PULL1);
4609
                                end
4610
                                else if (isPULU) begin
4611
                                        usp <= usp + 2'd1;
4612
                                        next_state(PULL1);
4613
                                end
4614
                                else if (isPULS) begin
4615
                                        ssp <= ssp + 2'd1;
4616
                                        next_state(PULL1);
4617
                                end
4618
                                else
4619
                                        next_state(IFETCH);
4620
                        end
4621
        `LW_YH:
4622
                        begin
4623
                                load_what <= `LW_YL;
4624
                                next_state(LOAD1);
4625
                                yr[`HIBYTE] <= dat;
4626
                                radr <= radr + 2'd1;
4627
                                if (isRTI) begin
4628
                                        $display("loadded YH=%h", dat);
4629
                                        ssp <= ssp + 2'd1;
4630
                                end
4631
                                else if (isPULU) begin
4632
                                        usp <= usp + 2'd1;
4633
                                end
4634
                                else if (isPULS) begin
4635
                                        ssp <= ssp + 2'd1;
4636
                                end
4637
                        end
4638
        `LW_YL: begin
4639
                                yr[`LOBYTE] <= dat;
4640
                                radr <= radr + 2'd1;
4641
                                if (isRTI) begin
4642
                                        $display("loadded YL=%h", dat);
4643
                                        ssp <= ssp + 2'd1;
4644
                                        next_state(PULL1);
4645
                                end
4646
                                else if (isPULU) begin
4647
                                        usp <= usp + 2'd1;
4648
                                        next_state(PULL1);
4649
                                end
4650
                                else if (isPULS) begin
4651
                                        ssp <= ssp + 2'd1;
4652
                                        next_state(PULL1);
4653
                                end
4654
                                else
4655
                                        next_state(IFETCH);
4656
                        end
4657
        `LW_USPH:       begin
4658
                                load_what <= `LW_USPL;
4659
                                next_state(LOAD1);
4660
                                usp[`HIBYTE] <= dat;
4661
                                radr <= radr + 2'd1;
4662
                                if (isRTI) begin
4663
                                        $display("loadded USPH=%h", dat);
4664
                                        ssp <= ssp + 2'd1;
4665
                                end
4666
                                else if (isPULS) begin
4667
                                        ssp <= ssp + 2'd1;
4668
                                end
4669
                        end
4670
        `LW_USPL:       begin
4671
                                usp[`LOBYTE] <= dat;
4672
                                radr <= radr + 2'd1;
4673
                                if (isRTI) begin
4674
                                        $display("loadded USPL=%h", dat);
4675
                                        ssp <= ssp + 2'd1;
4676
                                        next_state(PULL1);
4677
                                end
4678
                                else if (isPULS) begin
4679
                                        ssp <= ssp + 2'd1;
4680
                                        next_state(PULL1);
4681
                                end
4682
                                else
4683
                                        next_state(IFETCH);
4684
                        end
4685
        `LW_SSPH:       begin
4686
                                load_what <= `LW_SSPL;
4687
                                next_state(LOAD1);
4688
                                ssp[`HIBYTE] <= dat;
4689
                                radr <= radr + 2'd1;
4690
                                if (isRTI) begin
4691
                                        ssp <= ssp + 2'd1;
4692
                                end
4693
                                else if (isPULU) begin
4694
                                        usp <= usp + 2'd1;
4695
                                end
4696
                        end
4697
        `LW_SSPL:       begin
4698
                                ssp[`LOBYTE] <= dat;
4699
                                radr <= radr + 2'd1;
4700
                                if (isRTI) begin
4701
                                        ssp <= ssp + 2'd1;
4702
                                        next_state(PULL1);
4703
                                end
4704
                                else if (isPULU) begin
4705
                                        usp <= usp + 2'd1;
4706
                                        next_state(PULL1);
4707
                                end
4708
                                else
4709
                                        next_state(IFETCH);
4710
                        end
4711
        `LW_PCL:        begin
4712
                                pc[`LOBYTE] <= dat;
4713
                                radr <= radr + 2'd1;
4714 11 robfinch
                                // If loading from the vector table in bank zero, force pc[23:16]=0
4715
                                if (radr[`BYTE3]=={BPB{1'b0}} && radr[`BYTE2]=={BPB{1'b1}} && radr[7:4]==4'hF)
4716
                                        pc[`BYTE3] <= {BPB{1'b0}};
4717 15 robfinch
                                if (isRTI|isRTS|isRTF|isPULS) begin
4718 2 robfinch
                                        $display("loadded PCL=%h", dat);
4719
                                        ssp <= ssp + 2'd1;
4720
                                end
4721
                                else if (isPULU)
4722
                                        usp <= usp + 2'd1;
4723
                                next_state(IFETCH);
4724
                        end
4725
        `LW_PCH:        begin
4726
                                pc[`HIBYTE] <= dat;
4727
                                load_what <= `LW_PCL;
4728
                                radr <= radr + 2'd1;
4729 15 robfinch
                                if (isRTI|isRTS|isRTF|isPULS) begin
4730 2 robfinch
                                        $display("loadded PCH=%h", dat);
4731
                                        ssp <= ssp + 2'd1;
4732
                                end
4733
                                else if (isPULU)
4734
                                        usp <= usp + 2'd1;
4735
                                next_state(LOAD1);
4736
                        end
4737
        `LW_PC2316:     begin
4738
                                pc[`BYTE3] <= dat;
4739
                                load_what <= `LW_PCH;
4740
                                radr <= radr + 16'd1;
4741
                                if (isRTI|isRTF|isPULS)
4742
                                        ssp <= ssp + 16'd1;
4743
                                else if (isPULU)
4744
                                        usp <= usp + 16'd1;
4745
                                next_state(LOAD1);
4746
                        end
4747
        `LW_IAL:
4748
                        begin
4749
                                ia[`LOBYTE] <= dat;
4750
                                res[`LOBYTE] <= dat;
4751
                                radr <= {ia[`BYTE3],ia[`HIBYTE],dat};
4752
                                wadr <= {ia[`BYTE3],ia[`HIBYTE],dat};
4753
`ifdef SUPPORT_DBL_IND
4754
                                if (isDblIndirect) begin
4755
          load_what <= `LW_IAH;
4756
          next_state(LOAD1);
4757
          isDblIndirect <= `FALSE;
4758
                                end
4759
                                else
4760
`endif
4761
                                begin
4762
          load_what <= load_what2;
4763
          if (isOuterIndexed)
4764
              next_state(OUTER_INDEXING);
4765
          else begin
4766
              if (isLEA)
4767
                  next_state(IFETCH);
4768
              else if (isStore)
4769
                  next_state(STORE1);
4770
              else
4771
                  next_state(LOAD1);
4772
          end
4773
                                end
4774
                        end
4775
        `LW_IAH:
4776
                        begin
4777
                                ia[`HIBYTE] <= dat;
4778
                                res[`HIBYTE] <= dat;
4779
                                load_what <= `LW_IAL;
4780
                                radr <= radr + 2'd1;
4781
                                next_state(LOAD1);
4782
                        end
4783
        `LW_IA2316:
4784
                        begin
4785
                                ia[`BYTE3] <= dat;
4786
                                load_what <= `LW_IAH;
4787
                                radr <= radr + 32'd1;
4788
                                next_state(LOAD1);
4789
                        end
4790
        endcase
4791
end
4792
endtask
4793
 
4794
endmodule
4795
 
4796
// ============================================================================
4797
// Cache Memories
4798
// ============================================================================
4799
module rf6809_icachemem(wclk, wce, wr, wa, i, rclk, rce, pc, insn);
4800
input wclk;
4801
input wce;
4802
input wr;
4803
input [11:0] wa;
4804
input [BPB*16-1:0] i;
4805
input rclk;
4806
input rce;
4807
input [11:0] pc;
4808
output [`HEXBYTE] insn;
4809
reg [`HEXBYTE] insn;
4810
 
4811
integer n;
4812
reg [BPB*16-1:0] mem [0:255];
4813
reg [11:0] rpc,rpcp16;
4814
initial begin
4815
        for (n = 0; n < 256; n = n + 1)
4816
                mem[n] = {16{`NOP}};
4817
end
4818
 
4819
always_ff @(posedge wclk)
4820
        if (wce & wr) mem[wa[11:4]] <= i;
4821
 
4822
always_ff @(posedge rclk)
4823
        if (rce) rpc <= pc;
4824
always_ff @(posedge rclk)
4825
        if (rce) rpcp16 <= pc + 5'd16;
4826
wire [BPB*16-1:0] insn0 = mem[rpc[11:4]];
4827
wire [BPB*16-1:0] insn1 = mem[rpcp16[11:4]];
4828
always_comb
4829
        insn = {insn1,insn0} >> ({4'h0,rpc[3:0]} * BPB);
4830
 
4831
endmodule
4832
 
4833
module rf6809_itagmem(wclk, wce, wr, wa, invalidate, rclk, rce, pc, hit0, hit1);
4834
input wclk;
4835
input wce;
4836
input wr;
4837
input [`TRPBYTE] wa;
4838
input invalidate;
4839
input rclk;
4840
input rce;
4841
input [`TRPBYTE] pc;
4842
output hit0;
4843
output hit1;
4844
 
4845
integer n;
4846
reg [BPB*3-1:12] mem [0:255];
4847
reg [0:255] tvalid = 256'd0;
4848
reg [`TRPBYTE] rpc,rpcp16;
4849
wire [BPB*3-1:11] tag0,tag1;
4850
initial begin
4851
        for (n = 0; n < 256; n = n + 1)
4852
                mem[n] = {BPB*2{1'b0}};
4853
end
4854
 
4855
always_ff @(posedge wclk)
4856
        if (wce & wr) mem[wa[11:4]] <= wa[BPB*3-1:12];
4857
always_ff @(posedge wclk)
4858
        if (invalidate) tvalid <= 256'd0;
4859
        else if (wce & wr) tvalid[wa[11:4]] <= 1'b1;
4860
always_ff @(posedge rclk)
4861
        if (rce) rpc <= pc;
4862
always_ff @(posedge rclk)
4863
        if (rce) rpcp16 <= pc + 5'd16;
4864
assign tag0 = {mem[rpc[11:4]],tvalid[rpc[11:4]]};
4865
assign tag1 = {mem[rpcp16[11:4]],tvalid[rpcp16[11:4]]};
4866
 
4867
assign hit0 = tag0 == {rpc[BPB*3-1:12],1'b1};
4868 12 robfinch
// Consider a hit on port 1 if the instruction will not span onto it.
4869
assign hit1 = tag1 == {rpcp16[BPB*3-1:12],1'b1} || rpc[3:0] < 4'h9;
4870 2 robfinch
 
4871
endmodule
4872
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.