OpenCores
URL https://opencores.org/ocsvn/6809_6309_compatible_core/6809_6309_compatible_core/trunk

Subversion Repositories 6809_6309_compatible_core

[/] [6809_6309_compatible_core/] [trunk/] [rtl/] [verilog/] [MC6809_cpu.v] - Blame information for rev 6

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 ale500
/*
2
 *
3
 * MC6809/HD6309 Compatible code
4
 * (c) 2013 R.A. Paz Schmidt
5
 * distributed under the terms of the Lesser GPL, see LICENSE.TXT
6
 *
7
 */
8
 
9
`include "defs.v"
10
module MC6809_cpu(
11
        input  wire cpu_clk,
12
        input  wire cpu_reset,
13
        input  wire cpu_nmi_n,
14
        input  wire cpu_irq_n,
15
        input  wire cpu_firq_n,
16
        output wire [5:0] cpu_state_o,
17
        output wire cpu_we_o,
18
        output wire cpu_oe_o,
19
        output wire [15:0] cpu_addr_o,
20
        input  wire [7:0] cpu_data_i,
21
        output wire [7:0] cpu_data_o
22
        );
23
 
24
wire k_reset;
25
 
26 5 ale500
reg [7:0] k_opcode, k_postbyte, k_ind_ea; /* all bytes of an instruction */
27 6 ale500
reg [7:0] k_pp_regs; // push/pull registers to process
28
reg [3:0] k_pp_active_reg; // push/pull active register 
29 2 ale500
reg [7:0] k_memhi, k_memlo, k_cpu_data_o; /* operand read from memory */
30
reg [7:0] k_ofslo, k_ofshi, k_eahi, k_ealo;
31
reg [5:0] state, // state of the main state machine
32
          next_state, // next state to exit to from the read from [PC] state machine
33
                  next_mem_state, // next state to exit to from the read from memory state machine
34 5 ale500
                  next_push_state; // next state to exit to from push multiple state machine
35
reg k_write_tfr, k_write_exg;
36 4 ale500
reg k_cpu_oe, k_cpu_we, k_inc_pc;
37 2 ale500
reg [15:0] k_cpu_addr, k_new_pc;
38
reg k_write_pc, k_inc_su, k_dec_su, k_set_e, k_clear_e;
39 4 ale500
reg [1:0] k_mem_dest;
40 6 ale500
reg k_mul_cnt; // multiplier couner
41 4 ale500
reg k_write_dest; // set for 1 clock when a register has to be written, dec_o_dest_reg_addr has the register source
42
reg k_write_post_incdec; // asserted when in the last write cycle or in write back for loads
43
reg k_forced_mem_size; // used to force the size of a memory read to be 16 bits, used for vector fetch
44 2 ale500
/****
45
 * Decoder outputs
46
 */
47
wire [2:0] dec_o_p1_mode; // addressing mode
48
wire [2:0] dec_o_p1_optype; // k_opcode type
49
wire dec_o_use_s; // signals when S should be used instead of U
50
wire dec_o_alu_size;
51
/* ea decoder */
52
wire dec_o_ea_ofs8, dec_o_ea_ofs16, dec_o_ea_wpost, dec_o_ea_ofs0, dec_o_ea_indirect;
53
/* alu k_opcode decoder */
54
wire [4:0] dec_o_alu_opcode;
55
wire [1:0] dec_o_right_path_mod; /* Modifier for alu's right path input */
56
/* register decoder */
57 4 ale500
wire dec_o_wdest, dec_o_source_size, dec_o_write_flags;
58 2 ale500
wire [3:0] dec_o_left_path_addr, dec_o_right_path_addr, dec_o_dest_reg_addr;
59
/* test condition */
60
wire dec_o_cond_taken;
61
/* ALU outputs */
62
wire [15:0] alu_o_result;
63
wire [7:0] alu_o_CCR;
64 6 ale500
wire [7:0] alu8_o_result;
65
wire [7:0] alu8_o_CCR;
66 2 ale500
/* Register Module outputs */
67
wire [15:0] regs_o_left_path_data, regs_o_right_path_data, regs_o_eamem_addr, regs_o_su;
68
wire [7:0] regs_o_dp;
69
wire [15:0] regs_o_pc;
70
wire [7:0] regs_o_CCR;
71
/* Data Muxes */
72
reg [3:0] datamux_o_dest_reg_addr, datamux_o_alu_in_left_path_addr;
73
reg [15:0] datamux_o_alu_in_left_path_data, datamux_o_alu_in_right_path_data, datamux_o_dest;
74
 
75 5 ale500
reg k_p2_valid, k_p3_valid; /* 1 when k_postbyte has been loaded for page 2 or page 3 */
76 2 ale500
 
77 4 ale500
/*
78
 * Interrupt sync registers
79
 */
80 2 ale500
 
81
reg [2:0] k_reg_nmi, k_reg_irq, k_reg_firq;
82
wire k_nmi_req, k_firq_req, k_irq_req;
83
 
84
assign k_nmi_req = k_reg_nmi[2] & k_reg_nmi[1];
85
assign k_firq_req = k_reg_firq[2] & k_reg_firq[1];
86
assign k_irq_req = k_reg_irq[2] & k_reg_irq[1];
87
 
88 6 ale500
alu alu(
89
        .clk_in(cpu_clk),
90 2 ale500
        .a_in(datamux_o_alu_in_left_path_data),
91
        .b_in(datamux_o_alu_in_right_path_data),
92
        .CCR(regs_o_CCR), /* flags */
93 6 ale500
        .opcode_in(dec_o_alu_opcode), /* ALU k_opcode */
94
        .sz_in(dec_o_alu_size),
95 2 ale500
        .q_out(alu_o_result), /* ALU result */
96
        .CCRo(alu_o_CCR)
97 6 ale500
        );
98
 
99
 
100 2 ale500
regblock regs(
101 6 ale500
        .clk_in(cpu_clk),
102 2 ale500
        .path_left_addr(datamux_o_alu_in_left_path_addr),
103
        .path_right_addr(dec_o_right_path_addr),
104 5 ale500
        .write_reg_addr(datamux_o_dest_reg_addr),
105
        .exg_dest_r(k_postbyte[3:0]),
106 2 ale500
        .eapostbyte( k_ind_ea ),
107
        .offset16({ k_ofshi, k_ofslo }),
108 4 ale500
        .write_reg(k_write_dest),
109 5 ale500
        .write_tfr(k_write_tfr),
110
        .write_exg(k_write_exg),
111 2 ale500
        .write_post(k_write_post_incdec),
112
        .write_pc(k_write_pc),
113
        .inc_pc(k_inc_pc),
114
        .inc_su(k_inc_su),
115
        .dec_su(k_dec_su),
116
        .use_s(dec_o_use_s),
117
        .data_w(datamux_o_dest),
118
        .new_pc(k_new_pc),
119
        .CCR_in(alu_o_CCR),
120
        .write_flags(dec_o_write_flags & (state == `SEQ_GRAL_WBACK)),
121
        .set_e(k_set_e),
122
        .clear_e(k_clear_e),
123
        .CCR_o(regs_o_CCR),
124
        .path_left_data(regs_o_left_path_data),
125
        .path_right_data(regs_o_right_path_data),
126
        .eamem_addr(regs_o_eamem_addr),
127
        .reg_pc(regs_o_pc),
128
        .reg_dp(regs_o_dp),
129
        .reg_su(regs_o_su)
130
);
131
 
132
decode_regs dec_regs(
133
        .opcode(k_opcode),
134 5 ale500
        .postbyte0(k_postbyte),
135 2 ale500
        .page2_valid(k_p2_valid),
136
        .page3_valid(k_p3_valid),
137
        .path_left_addr(dec_o_left_path_addr),
138
        .path_right_addr(dec_o_right_path_addr),
139
        .dest_reg(dec_o_dest_reg_addr),
140 4 ale500
        .write_dest(dec_o_wdest),
141
        .source_size(dec_o_source_size),
142 2 ale500
        .result_size(dec_o_alu_size)
143
        );
144
 
145
decode_op dec_op(
146
        .opcode(k_opcode),
147 5 ale500
        .postbyte0(k_postbyte),
148 2 ale500
        .page2_valid(k_p2_valid),
149
        .page3_valid(k_p3_valid),
150
        .mode(dec_o_p1_mode),
151
        .optype(dec_o_p1_optype),
152
        .use_s(dec_o_use_s)
153
        );
154
 
155
decode_ea dec_ea(
156
        .eapostbyte( k_ind_ea ),
157
        .noofs(dec_o_ea_ofs0),
158
        .ofs8(dec_o_ea_ofs8),
159
        .ofs16(dec_o_ea_ofs16),
160
        .write_post(dec_o_ea_wpost),
161
        .isind(dec_o_ea_indirect)
162
        );
163
 
164
/* Opcodes for the ALU are decoded here
165
 * Write Flags are also decoded here
166
 */
167
decode_alu dec_alu(
168
        .opcode(k_opcode),
169 5 ale500
        .postbyte0(k_postbyte),
170 2 ale500
        .page2_valid(k_p2_valid),
171
        .page3_valid(k_p3_valid),
172
        .alu_opcode(dec_o_alu_opcode),
173
        .dec_alu_right_path_mod(dec_o_right_path_mod),
174
        .dest_flags(dec_o_write_flags)
175
        );
176
/* Condition decoder */
177
test_condition test_cond(
178
        .opcode(k_opcode),
179 5 ale500
        .postbyte0(k_postbyte),
180 2 ale500
        .page2_valid(k_p2_valid),
181
        .CCR(regs_o_CCR),
182
        .cond_taken(dec_o_cond_taken)
183
        );
184
 
185
/* Module IO */
186
 
187
assign cpu_oe_o = k_cpu_oe; // we latch on the rising edge
188
assign cpu_we_o = k_cpu_we;
189
assign cpu_addr_o = k_cpu_addr;
190
assign cpu_data_o = k_cpu_data_o;
191
assign k_reset = cpu_reset;
192
assign cpu_state_o = state;
193
 
194
 
195
/* Left Register read mux
196
 */
197
always @(*)
198 6 ale500
        begin
199
                if (k_pp_active_reg != `RN_INV)
200
                        datamux_o_alu_in_left_path_addr = k_pp_active_reg;
201
                else
202
                        datamux_o_alu_in_left_path_addr = dec_o_left_path_addr;
203
 
204
                /*
205 2 ale500
                case (k_pp_active_reg)
206
                        8'h80: datamux_o_alu_in_left_path_addr = `RN_PC;
207
                        8'h40: datamux_o_alu_in_left_path_addr = (dec_o_use_s) ? `RN_U:`RN_S;
208
                        8'h20: datamux_o_alu_in_left_path_addr = `RN_IY;
209
                        8'h10: datamux_o_alu_in_left_path_addr = `RN_IX;
210
                        8'h08: datamux_o_alu_in_left_path_addr = `RN_DP;
211
                        8'h04: datamux_o_alu_in_left_path_addr = `RN_ACCB;
212
                        8'h02: datamux_o_alu_in_left_path_addr = `RN_ACCA;
213 6 ale500
                        8'h01: datamux_o_alu_in_left_path_addr = `RN_CC;
214
                        8'h00: datamux_o_alu_in_left_path_addr = dec_o_left_path_addr;
215
                endcase
216
                */
217 2 ale500
        end
218
 
219
/* Destination register address MUX
220
 */
221
always @(*)
222 6 ale500
        begin
223
                if (k_pp_active_reg != `RN_INV)
224
                        datamux_o_dest_reg_addr = k_pp_active_reg;
225
                else
226
                        datamux_o_dest_reg_addr = dec_o_dest_reg_addr;
227
                /*
228 2 ale500
                case (k_pp_active_reg)
229
                        8'h80: datamux_o_dest_reg_addr = `RN_PC;
230
                        8'h40: datamux_o_dest_reg_addr = (dec_o_use_s) ? `RN_U:`RN_S;
231
                        8'h20: datamux_o_dest_reg_addr = `RN_IY;
232
                        8'h10: datamux_o_dest_reg_addr = `RN_IX;
233
                        8'h08: datamux_o_dest_reg_addr = `RN_DP;
234
                        8'h04: datamux_o_dest_reg_addr = `RN_ACCB;
235
                        8'h02: datamux_o_dest_reg_addr = `RN_ACCA;
236 6 ale500
                        8'h01: datamux_o_dest_reg_addr = `RN_CC;
237
                        8'h00: datamux_o_dest_reg_addr = dec_o_dest_reg_addr;
238
                endcase
239
                */
240 2 ale500
        end
241
 
242
/* Destination register data mux
243
 * selects the source to write to register. 16 bit registers have to be written at once after reading the low byte
244
 *
245
 */
246
always @(*)
247
        begin
248
                datamux_o_dest = alu_o_result;
249
                case (dec_o_p1_optype)
250
                        `OP_PULL, `OP_RTS: // destination register
251
                                datamux_o_dest = { k_memhi, k_memlo };
252
                        `OP_LEA:
253 4 ale500
                                if (dec_o_ea_indirect)// & dec_o_alu_size)
254 2 ale500
                                        datamux_o_dest = { k_memhi, k_memlo };
255
                                else
256
                                        datamux_o_dest = regs_o_eamem_addr;
257
                endcase
258
        end
259
 
260
/* ALU left input mux */
261
 
262
always @(*)
263
        begin
264
                if (dec_o_left_path_addr == `RN_MEM8)
265
                        datamux_o_alu_in_left_path_data = { k_memhi, k_memlo };
266
                else
267 4 ale500
                case (dec_o_p1_optype)
268
                        `OP_LEA:
269
                                if (dec_o_ea_indirect)// & dec_o_alu_size)
270
                                        datamux_o_alu_in_left_path_data = { k_memhi, k_memlo };
271
                                else
272
                                        datamux_o_alu_in_left_path_data = regs_o_eamem_addr;
273
                        default:
274 2 ale500
                                datamux_o_alu_in_left_path_data = regs_o_left_path_data;
275 4 ale500
                endcase
276
        end
277
/* PC as destination from jmp/bsr mux */
278
always @(*)
279
        begin
280
                k_new_pc = { k_memhi,k_memlo }; // used to fetch reset vector
281
                case (dec_o_p1_mode)
282
                        `REL16: k_new_pc = regs_o_pc + { k_memhi,k_memlo };
283
                        `REL8: k_new_pc = regs_o_pc + { {8{k_memlo[7]}}, k_memlo };
284
                        `EXTENDED: k_new_pc = { k_eahi,k_ealo };
285
                        `DIRECT: k_new_pc = { regs_o_dp, k_ealo };
286
                        `INDEXED:
287
                                if (dec_o_ea_indirect)
288
                                        k_new_pc = { k_memhi,k_memlo };
289
                                else
290
                                        k_new_pc = regs_o_eamem_addr;
291
 
292
                endcase
293 2 ale500
        end
294
/* ALU right input mux */
295
always @(*)
296
        begin
297
                case (dec_o_right_path_addr)
298
                        `RN_MEM8:
299
                                datamux_o_alu_in_right_path_data = { 8'h00, k_memlo };
300
                        `RN_MEM16:
301
                                datamux_o_alu_in_right_path_data = { k_memhi, k_memlo };
302
                        `RN_IMM8:
303
                                datamux_o_alu_in_right_path_data = { 8'h0, k_memlo };
304
                        `RN_IMM16:
305
                                datamux_o_alu_in_right_path_data = { k_memhi, k_memlo };
306
                        default:
307
                                case (dec_o_right_path_mod)
308
                                        `MOD_DEFAULT: datamux_o_alu_in_right_path_data = regs_o_right_path_data;
309
                                        `MOD_ONE: datamux_o_alu_in_right_path_data = 16'h0001;
310
                                        `MOD_ZERO: datamux_o_alu_in_right_path_data = 16'h0000;
311
                                        `MOD_MINUS1: datamux_o_alu_in_right_path_data = 16'hffff;
312
                                endcase
313
                endcase
314
        end
315
 
316 6 ale500
always @(posedge cpu_clk or posedge k_reset)
317 2 ale500
        begin
318
                if (k_reset == 1'b1)
319
                        begin
320
                                state <= `SEQ_COLDRESET;
321
                                k_reg_nmi <= 0;
322
                                k_reg_firq <= 0;
323
                                k_reg_irq <= 0;
324
                        end
325
                else
326
                        begin
327
                                /* Inrerrupt recognition and acknowledge */
328
                                if (!k_reg_nmi[2])
329
                                        k_reg_nmi <= { k_reg_nmi[1:0], cpu_nmi_n };
330
                                if (!k_reg_irq[2])
331
                                        k_reg_irq <= { k_reg_irq[1:0], cpu_irq_n };
332
                                if (!k_reg_firq[2])
333
                                        k_reg_firq <= { k_reg_firq[1:0], cpu_firq_n };
334
                                /* modifier registers */
335
                                if (k_inc_pc)
336
                                        k_inc_pc <= 0;
337
                                if (k_write_pc)
338
                                        k_write_pc <= 0;
339
                                if (k_cpu_we)
340
                                        k_cpu_we <= 0;
341
                                if (k_cpu_oe)
342
                                        k_cpu_oe <= 0;
343
                                if (k_write_post_incdec)
344
                                        k_write_post_incdec <= 0;
345
                                if (k_dec_su)
346
                                        k_dec_su <= 0;
347
                                if (k_inc_su)
348
                                        k_inc_su <= 0;
349
                                if (k_set_e)
350
                                        k_set_e <= 0;
351
                                if (k_clear_e)
352 4 ale500
                                        k_clear_e <= 0;
353
                                if (k_write_dest)
354 5 ale500
                                        k_write_dest <= 0;
355
                                if (k_write_exg)
356
                                        k_write_exg <= 0;
357
                                if (k_write_tfr)
358
                                        k_write_tfr <= 0;
359 2 ale500
                        case (state)
360
                                `SEQ_COLDRESET:
361 4 ale500
                                        begin
362
                                                k_forced_mem_size <= 1;
363 2 ale500
                                                state <= `SEQ_MEM_READ_H;
364
                                                k_eahi <= 8'hff;
365
                                                k_ealo <= 8'hfe;
366
                                                next_mem_state <= `SEQ_LOADPC;
367
                                        end
368
                                `SEQ_NMI:
369
                                        begin
370 4 ale500
                                                k_forced_mem_size <= 1;
371 2 ale500
                                                k_reg_nmi <= 2'h0;
372
                                                { k_eahi, k_ealo } <= 16'hfffc;
373
                                                k_pp_regs <= 8'hff;
374
                                                k_set_e <= 1;
375
                                                state <= `SEQ_PREPUSH; // first stack the registers
376
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
377
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
378
                                        end
379
                                `SEQ_SWI:
380
                                        begin
381 4 ale500
                                                k_forced_mem_size <= 1;
382 2 ale500
                                                state <= `SEQ_MEM_READ_H;
383
                                                { k_eahi, k_ealo } <= 16'hfffa;
384
                                                k_pp_regs <= 8'hff;
385
                                                state <= `SEQ_PREPUSH; // first stack the registers
386
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
387
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
388
                                                k_set_e <= 1;
389
                                        end
390
                                `SEQ_IRQ:
391
                                        begin
392 4 ale500
                                                k_forced_mem_size <= 1;
393 2 ale500
                                                k_reg_irq <= 2'h0;
394
                                                state <= `SEQ_MEM_READ_H;
395
                                                { k_eahi, k_ealo } <= 16'hfff8;
396
                                                k_pp_regs <= 8'hff;
397
                                                next_mem_state <= `SEQ_PREPUSH;
398
                                                k_set_e <= 1;
399
                                                state <= `SEQ_PREPUSH; // first stack the registers
400
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
401
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
402
                                        end
403
                                `SEQ_FIRQ:
404
                                        begin
405 4 ale500
                                                k_forced_mem_size <= 1;
406 2 ale500
                                                k_reg_firq <= 2'h0;
407
                                                { k_eahi, k_ealo } <= 16'hfff6;
408
                                                k_pp_regs <= 8'h81; // PC & CC
409
                                                k_clear_e <= 1;
410
                                                state <= `SEQ_PREPUSH; // first stack the registers
411
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
412
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
413
                                        end
414
                                `SEQ_SWI2:
415
                                        begin
416 4 ale500
                                                k_forced_mem_size <= 1;
417 2 ale500
                                                { k_eahi, k_ealo } <= 16'hfff4;
418
                                                k_pp_regs <= 8'hff;
419
                                                k_set_e <= 1;
420
                                                state <= `SEQ_PREPUSH; // first stack the registers
421
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
422
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
423
                                        end
424
                                `SEQ_SWI3:
425
                                        begin
426 4 ale500
                                                k_forced_mem_size <= 1;
427 2 ale500
                                                { k_eahi, k_ealo } <= 16'hfff2;
428
                                                k_pp_regs <= 8'hff;
429
                                                k_set_e <= 1;
430
                                                state <= `SEQ_PREPUSH; // first stack the registers
431
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
432
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
433
                                        end
434
                                `SEQ_UNDEF:
435
                                        begin
436 4 ale500
                                                k_forced_mem_size <= 1;
437 2 ale500
                                                { k_eahi, k_ealo } <= 16'hfff0;
438
                                                k_pp_regs <= 8'hff;
439
                                                k_set_e <= 1;
440
                                                state <= `SEQ_PREPUSH; // first stack the registers
441
                                                next_push_state <= `SEQ_MEM_READ_H; // than load new PC
442
                                                next_mem_state <= `SEQ_FETCH; // than continue fetching instructions
443
                                        end
444
                                `SEQ_LOADPC: /* loads the PC with the address taken from the reset vector */
445
                                        begin
446
                                                $display("cpu_data_i %02x %t", cpu_data_i, $time);
447
                                                state <= `SEQ_FETCH;
448
                                        end
449
                                `SEQ_FETCH: /* execution starts here */
450
                                        begin
451
                                                if (k_nmi_req)
452
                                                        state <= `SEQ_NMI;
453
                                                else
454
                                                if (k_firq_req)
455
                                                        state <= `SEQ_FIRQ;
456
                                                else
457
                                                if (k_irq_req)
458
                                                        state <= `SEQ_IRQ;
459
                                                else
460
                                                        begin
461
                                                                state <= `SEQ_FETCH_1;
462
                                                                k_cpu_addr <= regs_o_pc;
463
                                                        end
464
                                        end
465
                                `SEQ_FETCH_1:
466
                                        begin
467
                                                k_cpu_oe <= 1;
468 6 ale500
                                                k_inc_pc <= 1;
469
                                                k_p2_valid <= 0; // set when an k_opcode is page 2
470
                                                k_p3_valid <= 0; // set when an k_opcode is page 3
471
                                                k_pp_active_reg <= `RN_INV; // ensures that only push/pull control the left/dest muxes
472
                                                state <= `SEQ_FETCH_2;
473 2 ale500
                                        end
474
                                `SEQ_FETCH_2:
475
                                        begin
476
                                                k_opcode <= cpu_data_i;
477
                                                case (cpu_data_i[7:0]) /* page 2 & 3 opcodes are recognized here */
478
                                                        8'h10:
479
                                                                begin
480
                                                                        k_p2_valid <= 1;
481
                                                                        state <= `SEQ_FETCH_3;
482
                                                                end
483
                                                        8'h11:
484 6 ale500
                                                                begin
485 2 ale500
                                                                k_p3_valid <= 1;
486
                                                                state <= `SEQ_FETCH_3;
487 6 ale500
                                                                end
488 5 ale500
                                                        8'h1e, 8'h1f:
489 6 ale500
                                                                state <= `SEQ_FETCH_3; // tfr, exg, treated separately
490 2 ale500
                                                        default:
491
                                                                state <= `SEQ_DECODE;
492
                                                endcase
493
                                        end
494
                                `SEQ_FETCH_3:
495
                                        begin
496
                                                state <= `SEQ_FETCH_4;
497
                                                k_cpu_addr <= regs_o_pc;
498
                                        end
499
                                `SEQ_FETCH_4:
500
                                        begin
501
                                                k_cpu_oe <= 1;
502
                                                state <= `SEQ_FETCH_5;
503
                                        end
504
                                `SEQ_FETCH_5: /* fetches a page 2 or 3 opcode */
505
                                        begin
506 5 ale500
                                                k_postbyte <= cpu_data_i;
507 2 ale500
                                                k_inc_pc <= 1;
508
                                                state <= `SEQ_DECODE_P23;
509
                                        end
510
                                `SEQ_DECODE:
511
                                        begin
512
                                                /* here we have the first byte of the k_opcode and should be decided to which state we jump
513
                                                 * inherent means that no extra info is needed
514
                                                 * ALU opcodes need routing of registers to/from the ALU to the registers
515
                                                 */
516
                                                case (dec_o_p1_mode)
517 4 ale500
                                                        `NONE: // unknown k_opcode or push/pull... refetch ?
518 2 ale500
                                                                begin
519 6 ale500
                                                                        casex (k_opcode)
520
                                                                                8'h13: state <= `SEQ_SYNC;
521 2 ale500
                                                                                8'h39: // RTS
522 4 ale500
                                                                                        begin
523 2 ale500
                                                                                                state <= `SEQ_PREPULL;
524
                                                                                                k_pp_regs <= 8'h80; // Pull PC (RTS)all regs
525
                                                                                        end
526
                                                                                8'b001101x0: // PUSH S&U
527
                                                                                        begin
528
                                                                                                state <= `SEQ_PC_READ_L;
529
                                                                                                next_state <= `SEQ_PREPUSH;
530
                                                                                                next_push_state <= `SEQ_FETCH;
531
                                                                                        end
532
                                                                                8'b001101x1: // PULL S&U
533
                                                                                        begin
534
                                                                                                next_state <= `SEQ_PREPULL;
535
                                                                                                state <= `SEQ_PC_READ_L;
536
                                                                                        end
537 6 ale500
                                                                                8'h3B: // RTI
538
                                                                                        begin
539
                                                                                                state <= `SEQ_PREPULL;
540
                                                                                                k_pp_regs <= 8'hff; // all regs
541
                                                                                        end
542 2 ale500
                                                                                default: /* we ignore unknown opcodes */
543
                                                                                        state <= `SEQ_FETCH;
544
                                                                        endcase
545
                                                                end
546
                                                        `IMMEDIATE:     // 8 or 16 bits as result decides..
547
                                                                begin
548
                                                                        if (dec_o_alu_size)
549
                                                                                state <= `SEQ_PC_READ_H;
550
                                                                        else
551
                                                                                state <= `SEQ_PC_READ_L;
552
                                                                        next_state <= `SEQ_GRAL_ALU;
553
                                                                end
554
                                                        `INHERENT:
555
                                                                begin
556 6 ale500
                                                                        case (k_opcode)
557
                                                                                8'h3d: begin k_mul_cnt <= 1'h1; state <= `SEQ_GRAL_ALU; end // counter for mul
558 2 ale500
                                                                                8'h3f: state <= `SEQ_SWI;
559
                                                                                        default: state <= `SEQ_GRAL_ALU;
560
                                                                        endcase
561
                                                                end
562
                                                        `DIRECT:
563
                                                                begin
564
                                                                        state <= `SEQ_PC_READ_L;
565
                                                                        k_mem_dest <= `MEMDEST_MH; // operand to memlo/memhi
566
                                                                        if ((dec_o_right_path_addr == `RN_MEM8) || (dec_o_right_path_addr == `RN_MEM16) ||
567
                                                                                (dec_o_left_path_addr == `RN_MEM8))
568
                                                                                begin
569 4 ale500
                                                                                        next_state <= `SEQ_MEM_READ_H;
570 2 ale500
                                                                                        next_mem_state <= `SEQ_GRAL_ALU; // read then alu
571
                                                                                end
572
                                                                        else
573
                                                                                next_state <= `SEQ_GRAL_ALU; // no read
574
                                                                        k_eahi <= regs_o_dp;
575
                                                                end
576
                                                        `INDEXED:
577
                                                                state <= `SEQ_IND_READ_EA;
578
                                                        `EXTENDED:
579
                                                                begin
580
                                                                        state <= `SEQ_PC_READ_H; // loads address
581
                                                                        k_mem_dest <= `MEMDEST_MH; // operand to memlo/memhi
582
                                                                        if ((dec_o_right_path_addr == `RN_MEM8) || (dec_o_right_path_addr == `RN_MEM16) ||
583
                                                                                (dec_o_left_path_addr == `RN_MEM8))
584
                                                                                begin
585 4 ale500
                                                                                        next_state <= `SEQ_MEM_READ_H;
586 2 ale500
                                                                                        next_mem_state <= `SEQ_GRAL_ALU; // read then alu
587
                                                                                end
588
                                                                        else
589
                                                                                next_state <= `SEQ_GRAL_ALU; // no read
590
                                                                end
591
                                                        `REL8:
592
                                                                begin
593
                                                                        state <= `SEQ_PC_READ_L; // loads address
594
                                                                        if (dec_o_p1_optype == `OP_JSR) // bsr
595
                                                                                next_state <= `SEQ_JSR_PUSH;
596
                                                                        else
597
                                                                                next_state <= `SEQ_JMP_LOAD_PC; // offset loaded in this cycle, jump if needed
598
                                                                end
599
                                                        `REL16:
600
                                                                begin
601
                                                                        state <= `SEQ_PC_READ_H; // loads address
602
                                                                        if (dec_o_p1_optype == `OP_JSR) // lbsr
603
                                                                                next_state <= `SEQ_JSR_PUSH;
604
                                                                        else
605
                                                                                next_state <= `SEQ_JMP_LOAD_PC;
606
                                                                end
607
                                                endcase
608
                                        end
609
                                `SEQ_DECODE_P23:
610
                                        begin // has prefix 10 or 11
611
                                                case (dec_o_p1_mode)
612
                                                        `NONE: // unknown k_opcode... re-fetch ?
613
                                                                state <= `SEQ_FETCH;
614
                                                        `IMMEDIATE:     // 8 or 16 bits as result decides..
615 5 ale500
                                                                begin
616
                                                                        case (k_opcode)
617
                                                                                8'h1e: begin k_write_exg <= 1; state <= `SEQ_TFREXG; end
618
                                                                                8'h1f: begin k_write_tfr <= 1; state <= `SEQ_TFREXG; end
619
                                                                                default:
620
                                                                                        begin
621
                                                                                                next_state <= `SEQ_GRAL_ALU;
622
                                                                                                if (dec_o_alu_size)
623
                                                                                                        state <= `SEQ_PC_READ_H;
624
                                                                                                else
625
                                                                                                        state <= `SEQ_PC_READ_L;
626
                                                                                end
627
                                                                        endcase
628 2 ale500
                                                                end
629
                                                        `INHERENT:
630
                                                                case (k_opcode)
631
                                                                        8'h3f: if (k_p2_valid) state <= `SEQ_SWI2;
632
                                                                                   else state <= `SEQ_SWI3;
633
                                                                        default: state <= `SEQ_GRAL_ALU;
634
                                                                endcase
635
                                                        `DIRECT:
636
                                                                begin
637
                                                                        state <= `SEQ_PC_READ_L;
638
                                                                        k_mem_dest <= `MEMDEST_MH; // operand to memlo/memhi
639
                                                                        if ((dec_o_right_path_addr == `RN_MEM8) || (dec_o_right_path_addr == `RN_MEM16) ||
640
                                                                                (dec_o_left_path_addr == `RN_MEM8))
641
                                                                                begin
642 4 ale500
                                                                                        next_state <= `SEQ_MEM_READ_H;
643 2 ale500
                                                                                        next_mem_state <= `SEQ_GRAL_ALU; // read then alu
644
                                                                                end
645
                                                                        else
646
                                                                                next_state <= `SEQ_GRAL_ALU; // no read
647
                                                                        k_eahi <= regs_o_dp;
648
                                                                end
649
                                                        `INDEXED:
650
                                                                state <= `SEQ_IND_READ_EA;
651
                                                        `EXTENDED:
652
                                                                begin
653
                                                                        state <= `SEQ_PC_READ_H; // loads address
654
                                                                        k_mem_dest <= `MEMDEST_MH; // operand to memlo/memhi
655
                                                                        if ((dec_o_right_path_addr == `RN_MEM8) || (dec_o_right_path_addr == `RN_MEM16) ||
656
                                                                                (dec_o_left_path_addr == `RN_MEM8))
657
                                                                                begin
658 4 ale500
                                                                                        next_state <= `SEQ_MEM_READ_H;
659 2 ale500
                                                                                        next_mem_state <= `SEQ_GRAL_ALU; // read then alu
660
                                                                                end
661
                                                                        else
662
                                                                                next_state <= `SEQ_GRAL_ALU; // no read
663
                                                                end
664
                                                        `REL16:
665
                                                                begin // long branches only
666
                                                                        state <= `SEQ_PC_READ_H; // loads address
667
                                                                        next_state <= `SEQ_JMP_LOAD_PC;
668
                                                                end
669
                                                endcase
670
                                        end
671 4 ale500
                                `SEQ_GRAL_ALU:
672 6 ale500
                                        begin
673
                                                if (!k_mul_cnt)
674
                                                        begin
675
                                                                state <= `SEQ_GRAL_WBACK;
676
                                                                k_write_dest <= 1; /* write destination on wback */
677
                                                        end
678
                                                else
679
                                                        k_mul_cnt <= 1'h0;
680 4 ale500
                                        end
681 2 ale500
                                `SEQ_GRAL_WBACK:
682
                                        begin
683 4 ale500
                                                next_mem_state <= `SEQ_FETCH;
684 6 ale500
                                                casex (k_opcode)
685
                                                        8'h3C: state <= `SEQ_CWAI_STACK; // CWAI
686
                                                        default:
687
                                                                case (dec_o_dest_reg_addr)
688
                                                                        `RN_MEM8: state <= `SEQ_MEM_WRITE_L;
689
                                                                        `RN_MEM16: state <= `SEQ_MEM_WRITE_H;
690
                                                                        default:
691
                                                                                begin
692
                                                                                        state <= `SEQ_FETCH;
693
                                                                                        k_write_post_incdec <= dec_o_ea_wpost;
694
                                                                                end
695
                                                                endcase
696 2 ale500
                                                endcase
697
                                        end
698 6 ale500
                                `SEQ_CWAI_STACK:
699
                                        begin
700
                                                k_pp_regs <= 8'hff;
701
                                                k_set_e <= 1;
702
                                                state <= `SEQ_PREPUSH; // first stack the registers
703
                                                next_push_state <= `SEQ_CWAI_WAIT; // wait for interrupts
704
                                        end
705
                                `SEQ_CWAI_WAIT: /* waits for an interrupt and process it */
706
                                        begin
707
                                                k_forced_mem_size <= 1;
708
                                                next_mem_state <= `SEQ_FETCH; // then continue fetching instructions
709
                                                k_eahi <= 8'hff;
710
                                                k_ealo[7:4] <= 4'hf;
711
                                                if (k_nmi_req)
712
                                                        begin
713
                                                                k_reg_nmi <= 2'h0;
714
                                                                k_ealo[3:0] <= 4'hc;
715
                                                                state <= `SEQ_MEM_READ_H; // load new PC
716
                                                        end
717
                                                else
718
                                                if (k_firq_req)
719
                                                        begin
720
                                                                k_reg_firq <= 2'h0;
721
                                                                k_ealo[3:0] <= 4'h6;
722
                                                                state <= `SEQ_MEM_READ_H; // load new PC
723
                                                        end
724
                                                else
725
                                                if (k_irq_req)
726
                                                        begin
727
                                                                k_reg_irq <= 2'h0;
728
                                                                k_ealo[3:0] <= 4'hf8;
729
                                                                state <= `SEQ_MEM_READ_H; // load new PC
730
                                                        end
731
                                        end
732 5 ale500
                                `SEQ_TFREXG:
733
                                        state <= `SEQ_FETCH;
734 2 ale500
                                `SEQ_IND_READ_EA: // reads EA byte
735
                                        begin
736
                                                k_cpu_addr <= regs_o_pc;
737
                                                state <= `SEQ_IND_READ_EA_1;
738
                                                k_inc_pc <= 1;
739
                                        end
740
                                `SEQ_IND_READ_EA_1:
741
                                        begin
742
                                                k_cpu_oe <= 1; // read
743
                                                state <= `SEQ_IND_READ_EA_2;
744
                                        end
745
                                `SEQ_IND_READ_EA_2:
746
                                        begin
747
                                                k_ind_ea <= cpu_data_i;
748
                                                state <= `SEQ_IND_DECODE;
749
                                        end
750
                                `SEQ_IND_DECODE: // here we have to see what we need for indexed...
751
                                        begin
752
                                                if (dec_o_ea_ofs8)
753
                                                        begin // load 1 byte offset
754
                                                                state <= `SEQ_PC_READ_L;
755
                                                                next_state <= `SEQ_IND_DECODE_OFS; // has some offset, load arg
756
                                                        end
757
                                                else
758
                                                        if (dec_o_ea_ofs16)
759
                                                                begin // load 2 bytes offset
760
                                                                        state <= `SEQ_PC_READ_H;
761
                                                                        next_state <= `SEQ_IND_DECODE_OFS; // has some offset, load arg
762
                                                                end
763
                                                        else
764
                                                                //if (dec_o_ea_ofs0)
765
                                                                        begin // no extra load...
766
                                                                                if ((dec_o_right_path_addr == `RN_MEM8) || (dec_o_right_path_addr == `RN_MEM16) ||
767
                                                                                        (dec_o_left_path_addr == `RN_MEM8))
768
                                                                                        begin
769
                                                                                                k_mem_dest <= `MEMDEST_MH; // operand land in k_memhi/lo
770
                                                                                                next_mem_state <= `SEQ_GRAL_ALU;
771 4 ale500
                                                                                                state <= `SEQ_MEM_READ_H;
772 2 ale500
                                                                                        end
773
                                                                                else
774
                                                                                        state <= `SEQ_GRAL_ALU; // no load, then store
775
                                                                        end
776
                                        end
777
                                `SEQ_IND_DECODE_OFS: // loads argument if needed
778
                                        begin
779
                                                if ((dec_o_right_path_addr == `RN_MEM8) || (dec_o_right_path_addr == `RN_MEM16) ||
780
                                                        (dec_o_left_path_addr == `RN_MEM8))
781
                                                        begin
782
                                                                k_mem_dest <= `MEMDEST_MH; // operand land in k_memhi/lo
783
                                                                next_mem_state <= `SEQ_GRAL_ALU;
784 4 ale500
                                                                state <= `SEQ_MEM_READ_H;
785 2 ale500
                                                        end
786
                                                else
787
                                                        state <= `SEQ_GRAL_ALU; // no load, then store
788
                                        end
789
                                `SEQ_JMP_LOAD_PC:
790
                                        begin
791
                                                state <= `SEQ_FETCH;
792
                                        end
793
                                `SEQ_JSR_PUSH:
794
                                        begin
795
                                                k_pp_active_reg <= 8'h80; // push PC
796
                                                state <= `SEQ_PUSH_WRITE_L;
797
                                                next_state <= `SEQ_JMP_LOAD_PC;
798
                                        end
799
                                `SEQ_PREPUSH:
800
                                        begin
801
                                                next_state <= `SEQ_PREPUSH;
802 4 ale500
                                                if (k_pp_regs > 0)
803
                                                        begin
804
                                                                state <= `SEQ_PUSH_WRITE_L;
805
                                                        end
806 2 ale500
                                                else
807
                                                        state <= next_push_state;
808 6 ale500
                                                if (k_pp_regs[7]) begin k_pp_regs[7] <= 0; k_pp_active_reg <= `RN_PC; end
809 2 ale500
                                                else
810 6 ale500
                                                if (k_pp_regs[6]) begin k_pp_regs[6] <= 0; k_pp_active_reg <= (dec_o_use_s) ? `RN_U:`RN_S; end
811 2 ale500
                                                else
812 6 ale500
                                                if (k_pp_regs[5]) begin k_pp_regs[5] <= 0; k_pp_active_reg <= `RN_IY; end
813 2 ale500
                                                else
814 6 ale500
                                                if (k_pp_regs[4]) begin k_pp_regs[4] <= 0; k_pp_active_reg <= `RN_IX; end
815 2 ale500
                                                else
816 6 ale500
                                                if (k_pp_regs[3]) begin k_pp_regs[3] <= 0; k_pp_active_reg <= `RN_DP; end
817 2 ale500
                                                else
818 6 ale500
                                                if (k_pp_regs[2]) begin k_pp_regs[2] <= 0; k_pp_active_reg <= `RN_ACCB; end
819 2 ale500
                                                else
820 6 ale500
                                                if (k_pp_regs[1]) begin k_pp_regs[1] <= 0; k_pp_active_reg <= `RN_ACCA; end
821 2 ale500
                                                else
822 6 ale500
                                                if (k_pp_regs[0]) begin k_pp_regs[0] <= 0; k_pp_active_reg <= `RN_CC; end
823 2 ale500
                                        end
824
                                `SEQ_PREPULL:
825 4 ale500
                                        begin
826
                                                if (k_pp_regs != 8'h0)
827
                                                        begin
828
                                                                k_mem_dest <= `MEMDEST_MH;
829
                                                                next_mem_state <= `SEQ_PREPULL;
830
                                                        end
831
                                                else
832
                                                        state <= `SEQ_FETCH; // end of sequence
833 6 ale500
                                                if (k_pp_regs[0]) begin k_pp_active_reg <= `RN_CC; k_pp_regs[0] <= 0; state <= `SEQ_MEM_READ_L; end
834 2 ale500
                                                else
835 6 ale500
                                                if (k_pp_regs[1]) begin k_pp_active_reg <= `RN_ACCA; k_pp_regs[1] <= 0; state <= `SEQ_MEM_READ_L; end
836 2 ale500
                                                else
837 6 ale500
                                                if (k_pp_regs[2]) begin k_pp_active_reg <= `RN_ACCB; k_pp_regs[2] <= 0; state <= `SEQ_MEM_READ_L; end
838 2 ale500
                                                else
839 6 ale500
                                                if (k_pp_regs[3]) begin k_pp_active_reg <= `RN_DP; k_pp_regs[3] <= 0; state <= `SEQ_MEM_READ_L; end
840 2 ale500
                                                else
841 6 ale500
                                                if (k_pp_regs[4]) begin k_pp_active_reg <= `RN_IX; k_pp_regs[4] <= 0; state <= `SEQ_MEM_READ_H;end
842 2 ale500
                                                else
843 6 ale500
                                                if (k_pp_regs[5]) begin k_pp_active_reg <= `RN_IY; k_pp_regs[5] <= 0; state <= `SEQ_MEM_READ_H;end
844 2 ale500
                                                else
845 6 ale500
                                                if (k_pp_regs[6]) begin k_pp_active_reg <= (dec_o_use_s) ? `RN_U:`RN_S; k_pp_regs[6] <= 0; state <= `SEQ_MEM_READ_H; end
846 2 ale500
                                                else
847 6 ale500
                                                if (k_pp_regs[7]) begin k_pp_active_reg <= `RN_PC;  k_pp_regs[7] <= 0; state <= `SEQ_MEM_READ_H; end
848 2 ale500
                                        end
849
                                `SEQ_PUSH_WRITE_L: // first low byte push 
850
                                        begin
851
                                                k_cpu_data_o <= regs_o_left_path_data[7:0];
852
                                                state <= `SEQ_PUSH_WRITE_L_1;
853
                                                k_cpu_we <= 1; // write
854 4 ale500
                                                k_cpu_addr <= regs_o_su - 16'h1;
855
                                                k_dec_su <= 1;
856 2 ale500
                                        end
857
                                `SEQ_PUSH_WRITE_L_1:
858
                                        begin
859 6 ale500
                                                if (k_pp_active_reg < `RN_ACCA)
860 2 ale500
                                                        state <= `SEQ_PUSH_WRITE_H;
861
                                                else
862
                                                        if (k_pp_regs[3:0] > 0)
863
                                                                state <= `SEQ_PREPUSH;
864
                                                        else
865 4 ale500
                                                                state <= next_push_state;
866
                                                k_cpu_addr <= k_cpu_addr - 16'h1; // when pushing 16 bits the second decrement comes too late 
867 2 ale500
                                        end
868
                                `SEQ_PUSH_WRITE_H: // reads high byte
869
                                        begin
870
                                                k_cpu_data_o <= regs_o_left_path_data[15:8];
871
                                                state <= `SEQ_PUSH_WRITE_H_1;
872 4 ale500
                                                k_cpu_we <= 1; // write
873 6 ale500
                                                if (k_pp_active_reg >= `RN_ACCA)
874
                                                        k_cpu_addr <= regs_o_su; // address for 8 bit register
875 2 ale500
                                                k_dec_su <= 1; // decrement stack pointer
876
                                        end
877
                                `SEQ_PUSH_WRITE_H_1:
878 4 ale500
                                        begin
879
                                                if (next_state == `SEQ_JMP_LOAD_PC)
880
                                                        k_write_pc <= 1; // load PC in the next cycle, the mux output will have the right source
881 2 ale500
                                                state <= next_state;
882
                                        end
883
                                `SEQ_PC_READ_H: // reads high byte for [PC], used by IMM, DIR, EXT
884
                                        begin
885
                                                k_cpu_addr <= regs_o_pc;
886
                                                state <= `SEQ_PC_READ_H_1;
887
                                                k_inc_pc <= 1;
888
                                        end
889
                                `SEQ_PC_READ_H_1:
890
                                        begin
891
                                                k_cpu_oe <= 1; // read
892
                                                state <= `SEQ_PC_READ_H_2;
893
                                        end
894
                                `SEQ_PC_READ_H_2:
895
                                        begin
896
                                                case (dec_o_p1_mode)
897
                                                        `REL16, `IMMEDIATE: k_memhi <= cpu_data_i;
898
                                                        `EXTENDED: k_eahi <= cpu_data_i;
899
                                                        `INDEXED: k_ofshi <= cpu_data_i;
900
                                                endcase
901
                                                state <= `SEQ_PC_READ_L;
902
                                        end
903
                                `SEQ_PC_READ_L: // reads low byte [PC]
904
                                        begin
905
                                                k_cpu_addr <= regs_o_pc;
906
                                                state <= `SEQ_PC_READ_L_1;
907
                                                k_inc_pc <= 1;
908
                                        end
909
                                `SEQ_PC_READ_L_1:
910
                                        begin
911
                                                k_cpu_oe <= 1; // read
912
                                                state <= `SEQ_PC_READ_L_2;
913
                                        end
914
                                `SEQ_PC_READ_L_2:
915
                                        begin
916
                                                case (dec_o_p1_mode)
917
                                                        `NONE: k_pp_regs <= cpu_data_i; // push & pull
918
                                                        `REL8, `REL16, `IMMEDIATE: k_memlo <= cpu_data_i;
919
                                                        `DIRECT, `EXTENDED: k_ealo <= cpu_data_i;
920
                                                        `INDEXED: k_ofslo <= cpu_data_i;
921 4 ale500
                                                endcase
922
                                                if ((next_state == `SEQ_JMP_LOAD_PC) & (dec_o_cond_taken))
923
                                                        k_write_pc <= 1; // load PC in the next cycle, the mux output will have the right source
924 2 ale500
                                                state <= next_state;
925
                                        end
926
                                `SEQ_MEM_READ_H: // reads high byte
927
                                        begin
928
                                                case (dec_o_p1_mode)
929
                                                        `NONE: begin k_cpu_addr <= regs_o_su; k_inc_su <= 1; end // pull, rts, rti
930
                                                        `INDEXED: k_cpu_addr <= regs_o_eamem_addr;
931
                                                        default: k_cpu_addr <= { k_eahi, k_ealo };
932 4 ale500
                                                endcase
933 6 ale500
                                                if (k_forced_mem_size | dec_o_source_size | (k_pp_active_reg <  `RN_ACCA))
934 4 ale500
                                                        state <= `SEQ_MEM_READ_H_1;
935
                                                else
936
                                                        state <= `SEQ_MEM_READ_L_1;
937
                                                k_forced_mem_size <= 0; // used for vector fetch
938 2 ale500
                                        end
939
                                `SEQ_MEM_READ_H_1:
940
                                        begin
941
                                                k_cpu_oe <= 1; // read
942
                                                state <= `SEQ_MEM_READ_H_2;
943
                                        end
944
                                `SEQ_MEM_READ_H_2:
945
                                        begin
946
                                                case (k_mem_dest)
947 4 ale500
                                                        `MEMDEST_PC,//: k_new_pc[15:8] <= cpu_data_i;
948 2 ale500
                                                        `MEMDEST_MH: k_memhi <= cpu_data_i;
949
                                                        `MEMDEST_AH: k_eahi <= cpu_data_i;
950
                                                endcase
951
                                                state <= `SEQ_MEM_READ_L_1;
952 4 ale500
                                                k_cpu_addr  <= k_cpu_addr + 16'h1;
953
                                                case (dec_o_p1_mode)
954
                                                        `NONE: begin k_inc_su <= 1; end // pull, rts, rti
955
                                                endcase
956 2 ale500
                                        end
957 4 ale500
                                `SEQ_MEM_READ_L: // reads low byte
958 2 ale500
                                        begin
959 4 ale500
                                                // falls through from READ_MEM_H with the right address
960 2 ale500
                                                case (dec_o_p1_mode)
961 4 ale500
                                                        `NONE: begin k_cpu_addr <= regs_o_su; k_inc_su <= 1; end // pull, rts, rti
962 2 ale500
                                                endcase
963
                                                state <= `SEQ_MEM_READ_L_1;
964
                                        end
965
                                `SEQ_MEM_READ_L_1:
966
                                        begin
967
                                                k_cpu_oe <= 1; // read
968
                                                state <= `SEQ_MEM_READ_L_2;
969
                                        end
970
                                `SEQ_MEM_READ_L_2:
971
                                        begin
972
                                                case (k_mem_dest)
973 4 ale500
                                                        `MEMDEST_PC: begin k_memlo <= cpu_data_i; k_write_pc <= 1; end
974 2 ale500
                                                        `MEMDEST_MH: k_memlo <= cpu_data_i;
975
                                                        `MEMDEST_AH: k_ealo <= cpu_data_i;
976
                                                endcase
977
                                                case (dec_o_p1_mode)
978 4 ale500
                                                        `NONE, `INHERENT: k_write_dest <= 1; // pull, rts, rti
979 2 ale500
                                                endcase
980
                                                state <= next_mem_state;
981
                                        end
982
                                `SEQ_MEM_WRITE_H: // writes high byte
983
                                        begin
984
                                                case (dec_o_p1_mode)
985
                                                        `INDEXED: k_cpu_addr <= regs_o_eamem_addr;
986
                                                        default: k_cpu_addr <= { k_eahi, k_ealo };
987
                                                endcase
988 4 ale500
                                                k_cpu_data_o <= datamux_o_dest[15:8];
989 2 ale500
                                                state <= `SEQ_MEM_WRITE_H_1;
990
                                                k_cpu_we <= 1; // read
991
                                        end
992
                                `SEQ_MEM_WRITE_H_1:
993
                                        begin
994
                                                state <= `SEQ_MEM_WRITE_L;
995
                                                k_cpu_addr <= k_cpu_addr + 16'h1;
996
                                        end
997
                                `SEQ_MEM_WRITE_L: // reads high byte
998
                                        begin
999 6 ale500
                                                if (!dec_o_alu_size) // only if it is an 8 bit write
1000 2 ale500
                                                        case (dec_o_p1_mode)
1001
                                                                `INDEXED: k_cpu_addr <= regs_o_eamem_addr;
1002
                                                                default: k_cpu_addr <= { k_eahi, k_ealo };
1003
                                                        endcase
1004
                                                k_cpu_data_o <= datamux_o_dest[7:0];
1005
                                                state <= `SEQ_MEM_WRITE_L_1;
1006
                                                k_cpu_we <= 1; // write
1007
                                        end
1008
                                `SEQ_MEM_WRITE_L_1:
1009
                                        begin
1010
                                                k_write_post_incdec <= dec_o_ea_wpost;
1011
                                                state <= next_mem_state;
1012
                                        end
1013
 
1014
                        endcase
1015
                end
1016
        end
1017
 
1018
initial
1019
        begin
1020
                k_cpu_oe = 0;
1021
                k_cpu_we = 0;
1022
                k_mem_dest = 0;
1023 5 ale500
                k_new_pc = 16'hffff;
1024
                k_write_tfr = 0;
1025 6 ale500
                k_write_exg = 0;
1026
                k_mul_cnt = 0;
1027 2 ale500
        end
1028
endmodule

powered by: WebSVN 2.1.0

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