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

Subversion Repositories t6507lp

[/] [t6507lp/] [trunk/] [rtl/] [verilog/] [t6507lp_fsm.v] - Blame information for rev 101

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

Line No. Rev Author Line
1 61 creep
////////////////////////////////////////////////////////////////////////////
2
////                                                                    ////
3
//// T6507LP IP Core                                                    ////
4
////                                                                    ////
5
//// This file is part of the T6507LP project                           ////
6
//// http://www.opencores.org/cores/t6507lp/                            ////
7
////                                                                    ////
8
//// Description                                                        ////
9
//// 6507 FSM                                                           ////
10
////                                                                    ////
11
//// TODO:                                                              ////
12 96 creep
//// - Fix relative mode, bit 7 means negative                          ////
13 61 creep
//// - Code the indirect indexed mode                                   ////
14
//// - Code the absolute indirect mode                                  ////
15
////                                                                    ////
16
//// Author(s):                                                         ////
17
//// - Gabriel Oshiro Zardo, gabrieloshiro@gmail.com                    ////
18
//// - Samuel Nascimento Pagliarini (creep), snpagliarini@gmail.com     ////
19
////                                                                    ////
20
////////////////////////////////////////////////////////////////////////////
21
////                                                                    ////
22
//// Copyright (C) 2001 Authors and OPENCORES.ORG                       ////
23
////                                                                    ////
24
//// This source file may be used and distributed without               ////
25
//// restriction provided that this copyright statement is not          ////
26
//// removed from the file and that any derivative work contains        ////
27
//// the original copyright notice and the associated disclaimer.       ////
28
////                                                                    ////
29
//// This source file is free software; you can redistribute it         ////
30
//// and/or modify it under the terms of the GNU Lesser General         ////
31
//// Public License as published by the Free Software Foundation;       ////
32
//// either version 2.1 of the License, or (at your option) any         ////
33
//// later version.                                                     ////
34
////                                                                    ////
35
//// This source is distributed in the hope that it will be             ////
36
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
37
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
38
//// PURPOSE. See the GNU Lesser General Public License for more        ////
39
//// details.                                                           ////
40
////                                                                    ////
41
//// You should have received a copy of the GNU Lesser General          ////
42
//// Public License along with this source; if not, download it         ////
43
//// from http://www.opencores.org/lgpl.shtml                           ////
44
////                                                                    ////
45
////////////////////////////////////////////////////////////////////////////
46
 
47
`timescale 1ns / 1ps
48
 
49 86 creep
module t6507lp_fsm(clk, reset_n, alu_result, alu_status, data_in, address, control, data_out, alu_opcode, alu_a, alu_enable, alu_x, alu_y);
50 87 creep
        parameter DATA_SIZE = 8;
51
        parameter ADDR_SIZE = 13;
52 68 creep
 
53 83 creep
        localparam DATA_SIZE_ = DATA_SIZE - 4'b0001;
54
        localparam ADDR_SIZE_ = ADDR_SIZE - 4'b0001;
55 82 creep
 
56 71 creep
        input clk;
57
        input reset_n;
58 82 creep
        input [DATA_SIZE_:0] alu_result;
59
        input [DATA_SIZE_:0] alu_status;
60
        input [DATA_SIZE_:0] data_in;
61
        output reg [ADDR_SIZE_:0] address;
62 61 creep
        output reg control; // one bit is enough? read = 0, write = 1
63 82 creep
        output reg [DATA_SIZE_:0] data_out;
64
        output reg [DATA_SIZE_:0] alu_opcode;
65
        output reg [DATA_SIZE_:0] alu_a;
66 68 creep
        output reg alu_enable;
67 61 creep
 
68 86 creep
        input [DATA_SIZE_:0] alu_x;
69
        input [DATA_SIZE_:0] alu_y;
70 68 creep
 
71 61 creep
        // FSM states
72 95 creep
        localparam FETCH_OP = 5'b00000;
73
        localparam FETCH_OP_CALC = 5'b00001;
74
        localparam FETCH_LOW = 5'b00010;
75
        localparam FETCH_HIGH = 5'b00011;
76
        localparam READ_MEM = 5'b00100;
77
        localparam DUMMY_WRT_CALC = 5'b00101;
78
        localparam WRITE_MEM = 5'b00110;
79
        localparam FETCH_OP_CALC_PARAM = 5'b00111;
80
        localparam READ_MEM_CALC_INDEX = 5'b01000;
81
        localparam FETCH_HIGH_CALC_INDEX = 5'b01001;
82
        localparam READ_MEM_FIX_ADDR = 5'b01010;
83
        localparam FETCH_OP_EVAL_BRANCH = 5'b01011;
84
        localparam FETCH_OP_FIX_PC = 5'b01100;
85
        localparam READ_FROM_POINTER = 5'b01101;
86
        localparam READ_FROM_POINTER_X = 5'b01110;
87
        localparam READ_FROM_POINTER_X1 = 5'b01111;
88 61 creep
 
89 95 creep
        localparam RESET = 5'b11111;
90
 
91 61 creep
        // OPCODES TODO: verify how this get synthesised
92
        `include "../T6507LP_Package.v"
93
 
94
        // control signals
95
        localparam MEM_READ = 1'b0;
96
        localparam MEM_WRITE = 1'b1;
97
 
98 82 creep
        reg [ADDR_SIZE_:0] pc;           // program counter
99
        reg [DATA_SIZE_:0] sp;           // stack pointer
100
        reg [DATA_SIZE_:0] ir;           // instruction register
101
        reg [ADDR_SIZE_:0] temp_addr;    // temporary address
102
        reg [DATA_SIZE_:0] temp_data;    // temporary data
103 61 creep
 
104 96 creep
        reg [4:0] state, next_state; // current and next state registers
105 61 creep
        // TODO: not sure if this will be 4 bits wide. as of march 9th this was 4bit wide.
106
 
107
        // wiring that simplifies the FSM logic
108
        reg absolute;
109
        reg absolute_indexed;
110
        reg accumulator;
111
        reg immediate;
112
        reg implied;
113 95 creep
        reg indirectx;
114
        reg indirecty;
115 61 creep
        reg relative;
116
        reg zero_page;
117
        reg zero_page_indexed;
118 86 creep
        reg [DATA_SIZE_:0] index; // will be assigned with either X or Y
119 61 creep
 
120
        // regs that store the type of operation. again, this simplifies the FSM a lot.
121
        reg read;
122
        reg read_modify_write;
123
        reg write;
124
        reg jump;
125 101 creep
        reg jump_indirect;
126 61 creep
 
127 82 creep
        wire [ADDR_SIZE_:0] next_pc;
128 63 creep
        assign next_pc = pc + 13'b0000000000001;
129 61 creep
 
130 87 creep
        reg [ADDR_SIZE_:0] address_plus_index; // this would update more times than actually needed, consuming power.
131
        reg page_crossed;                       // so the simple assign was changed into a combinational always block
132
 
133 94 creep
        reg branch;
134
 
135 87 creep
        always @(*) begin
136
                address_plus_index = 0;
137
                page_crossed = 0;
138 86 creep
 
139 88 creep
                if (state == READ_MEM_CALC_INDEX || state == READ_MEM_FIX_ADDR || state == FETCH_HIGH_CALC_INDEX) begin
140
                        {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
141
                        address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
142 87 creep
                end
143 94 creep
                else if (branch) begin
144
                        if (state == FETCH_OP_FIX_PC || state == FETCH_OP_EVAL_BRANCH) begin
145
                                {page_crossed, address_plus_index[7:0]} = pc[7:0] + index;
146
                                address_plus_index[12:8] = pc[12:8] + page_crossed;// warning: pc might feed these lines twice and cause branch failure
147
                        end                                                             // solution: add a temp reg i guess
148
                end
149 95 creep
                else if (state == READ_FROM_POINTER) begin
150 96 creep
                        if (indirectx) begin
151
                                {page_crossed, address_plus_index[7:0]} = temp_data + index;
152
                                address_plus_index[12:8] = 5'b00000;
153
                        end
154 101 creep
                        else if (jump_indirect) begin
155
                                address_plus_index[7:0] = temp_addr + 8'h01;
156
                                address_plus_index[12:8] = 5'b00000;
157
                        end
158 96 creep
                        else begin // indirecty falls here
159
                                address_plus_index[7:0] = temp_data + 8'h01;
160
                                address_plus_index[12:8] = 5'b00000;
161
                        end
162 95 creep
                end
163
                else if (state == READ_FROM_POINTER_X) begin
164
                        {page_crossed, address_plus_index[7:0]} = temp_data + index + 8'h01;
165
                        address_plus_index[12:8] = 5'b00000;
166
                end
167 96 creep
                else if (state == READ_FROM_POINTER_X1) begin
168
                        {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
169
                        address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
170
                end
171 87 creep
        end
172
 
173 71 creep
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
174
                if (reset_n == 1'b0) begin
175
                        // all registers must assume default values
176 68 creep
                        pc <= 0; // TODO: this is written somewhere. something about a reset vector. must be checked.
177
                        sp <= 0; // TODO: the default is not 0. maybe $0100 or something like that. must be checked.
178 82 creep
                        ir <= 8'h00;
179 71 creep
                        temp_addr <= 0;
180 82 creep
                        temp_data <= 8'h00;
181 68 creep
                        state <= RESET;
182 71 creep
                        // registered outputs also receive default values
183
                        address <= 0;
184 82 creep
                        control <= MEM_READ;
185
                        data_out <= 8'h00;
186 61 creep
                end
187
                else begin
188
                        state <= next_state;
189 83 creep
 
190 61 creep
                        case (state)
191 68 creep
                                RESET: begin
192 82 creep
                                        // The processor was reset
193
                                        $write("under reset");
194 68 creep
                                end
195 61 creep
                                FETCH_OP: begin // this state is the simplest one. it is a simple fetch that must be done when the cpu was reset or
196
                                                // the last cycle was a memory write.
197
                                        pc <= next_pc;
198 71 creep
                                        address <= next_pc;
199 83 creep
                                        control <= MEM_READ;
200 70 creep
                                        ir <= data_in;
201 61 creep
                                end
202 71 creep
                                FETCH_OP_CALC, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
203 61 creep
                                        pc <= next_pc;
204 71 creep
                                        address <= next_pc;
205 83 creep
                                        control <= MEM_READ;
206 70 creep
                                        ir <= data_in;
207 61 creep
                                end
208 68 creep
                                FETCH_LOW: begin // in this state the opcode is already known so truly execution begins
209
                                        if (accumulator || implied) begin
210 70 creep
                                                pc <= pc; // is this better?
211 71 creep
                                                address <= pc;
212 83 creep
                                                control <= MEM_READ;
213 61 creep
                                        end
214 94 creep
                                        else if (immediate || relative) begin
215 68 creep
                                                pc <= next_pc;
216 71 creep
                                                address <= next_pc;
217 83 creep
                                                control <= MEM_READ;
218 70 creep
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
219 61 creep
                                        end
220 101 creep
                                        else if (absolute || absolute_indexed || jump_indirect) begin
221 71 creep
                                                pc <= next_pc;
222
                                                address <= next_pc;
223 83 creep
                                                control <= MEM_READ;
224 87 creep
                                                temp_addr <= {{5{1'b0}},data_in};
225 101 creep
                                                temp_data <= 8'h00;
226 71 creep
                                        end
227 77 creep
                                        else if (zero_page) begin
228
                                                pc <= next_pc;
229
                                                address <= {{5{1'b0}},data_in};
230 78 creep
                                                temp_addr <= {{5{1'b0}},data_in};
231
 
232 77 creep
                                                if (write) begin
233
                                                        control <= MEM_WRITE;
234 78 creep
                                                        data_out <= alu_result;
235 77 creep
                                                end
236 83 creep
                                                else begin
237
                                                        control <= MEM_READ;
238
                                                        data_out <= 8'h00;
239
                                                end
240 77 creep
                                        end
241 86 creep
                                        else if (zero_page_indexed) begin
242
                                                pc <= next_pc;
243 94 creep
                                                address <= {{5{1'b0}}, data_in};
244
                                                temp_addr <= {{5{1'b0}}, data_in};
245 86 creep
                                                control <= MEM_READ;
246
                                        end
247 96 creep
                                        else if (indirectx || indirecty) begin
248 95 creep
                                                pc <= next_pc;
249
                                                address <= data_in;
250
                                                temp_data <= data_in;
251
                                                control <= MEM_READ;
252
                                        end
253 61 creep
                                end
254 87 creep
                                FETCH_HIGH_CALC_INDEX: begin
255
                                        pc <= next_pc;
256
                                        temp_addr[12:8] <= data_in[4:0];
257
                                        address <= {data_in[4:0], address_plus_index[7:0]};
258
                                        control <= MEM_READ;
259
                                        data_out <= 8'h00;
260
                                end
261 94 creep
                                FETCH_OP_EVAL_BRANCH: begin
262
                                        if (branch) begin
263
                                                pc <= {{5{1'b0}}, address_plus_index[7:0]};
264
                                                address <= {{5{1'b0}}, address_plus_index[7:0]};
265
                                                control <= MEM_READ;
266
                                                data_out <= 8'h00;
267
                                        end
268
                                        else begin
269
                                                pc <= next_pc;
270
                                                address <= next_pc;
271
                                                control <= MEM_READ;
272
                                                data_out <= 8'h00;
273 95 creep
                                                ir <= data_in;
274 94 creep
                                        end
275
                                end
276
                                FETCH_OP_FIX_PC: begin
277
                                        if (page_crossed) begin
278
                                                pc[12:8] <= address_plus_index[12:8];
279
                                                address[12:8] <= address_plus_index[12:8];
280
                                        end
281
                                        else begin
282
                                                pc <= next_pc;
283
                                                address <= next_pc;
284
                                                control <= MEM_READ;
285
                                                ir <= data_in;
286
                                        end
287
                                end
288 71 creep
                                FETCH_HIGH: begin
289
                                        if (jump) begin
290 83 creep
                                                pc <= {data_in[4:0], temp_addr[7:0]}; // PCL <= first byte, PCH <= second byte
291
                                                address <= {data_in[4:0], temp_addr[7:0]};
292
                                                control <= MEM_READ;
293
                                                data_out <= 8'h00;
294 71 creep
                                        end
295
                                        else begin
296
                                                if (write) begin
297
                                                        pc <= next_pc;
298
                                                        temp_addr[12:8] <= data_in[4:0];
299
                                                        address <= {data_in[4:0],temp_addr[7:0]};
300
                                                        control <= MEM_WRITE;
301 77 creep
                                                        data_out <= alu_result;
302 71 creep
                                                end
303
                                                else begin // read_modify_write or just read
304
                                                        pc <= next_pc;
305
                                                        temp_addr[12:8] <= data_in[4:0];
306
                                                        address <= {data_in[4:0],temp_addr[7:0]};
307 83 creep
                                                        control <= MEM_READ;
308
                                                        data_out <= 8'h00;
309 71 creep
                                                end
310
                                        end
311
                                        //else begin
312
                                        //      $write("FETCHHIGH PROBLEM"); 
313
                                        //      $finish(0); 
314
                                        //end
315 61 creep
                                end
316 71 creep
                                READ_MEM: begin
317
                                        if (read_modify_write) begin
318
                                                pc <= pc;
319
                                                address <= temp_addr;
320
                                                control <= MEM_WRITE;
321
                                                temp_data <= data_in;
322
                                                data_out <= data_in; // writeback the same value
323
                                        end
324
                                        else begin
325
                                                pc <= pc;
326
                                                address <= pc;
327
                                                temp_data <= data_in;
328 83 creep
                                                control <= MEM_READ;
329
                                                data_out <= 8'h00;
330 71 creep
                                        end
331 70 creep
                                end
332 86 creep
                                READ_MEM_CALC_INDEX: begin
333
                                                //pc <= next_pc; // pc was  already updated in the previous cycle
334
                                                address <= address_plus_index;
335
                                                temp_addr <= address_plus_index;
336
 
337
                                                if (write) begin
338
                                                        control <= MEM_WRITE;
339
                                                        data_out <= alu_result;
340
                                                end
341
                                                else begin
342
                                                        control <= MEM_READ;
343
                                                        data_out <= 8'h00;
344
                                                end
345
 
346
                                end
347 87 creep
                                READ_MEM_FIX_ADDR: begin
348
                                        if (read) begin
349
                                                control <= MEM_READ;
350
                                                data_out <= 8'h00;
351
 
352
                                                if (page_crossed) begin
353
                                                        address <= address_plus_index;
354
                                                        temp_addr <= address_plus_index;
355
                                                end
356
                                                else begin
357
                                                        address <= pc;
358
                                                        temp_data <= data_in;
359
                                                end
360
                                        end
361
                                        else if (write) begin
362
                                                control <= MEM_WRITE;
363
                                                data_out <= alu_result;
364
                                                address <= address_plus_index;
365
                                                temp_addr <= address_plus_index;
366
 
367
                                        end
368
                                        else begin // read modify write
369
                                                control <= MEM_READ;
370
                                                data_out <= 8'h00;
371
                                                address <= address_plus_index;
372
                                                temp_addr <= address_plus_index;
373
                                        end
374
                                end
375 71 creep
                                DUMMY_WRT_CALC: begin
376
                                        pc <= pc;
377
                                        address <= temp_addr;
378
                                        control <= MEM_WRITE;
379
                                        data_out <= alu_result;
380 70 creep
                                end
381 71 creep
                                WRITE_MEM: begin
382
                                        pc <= pc;
383
                                        address <= pc;
384 83 creep
                                        control <= MEM_READ;
385
                                        data_out <= 8'h00;
386 70 creep
                                end
387 95 creep
                                READ_FROM_POINTER: begin
388 101 creep
                                        if (jump_indirect) begin
389
                                                pc[7:0] <= data_in;
390
                                                control <= MEM_READ;
391 96 creep
                                                address <= address_plus_index;
392
                                        end
393 101 creep
                                        else begin
394
                                                pc <= pc;
395
                                                control <= MEM_READ;
396
 
397
                                                if (indirectx) begin
398
                                                        address <= address_plus_index;
399
                                                end
400
                                                else begin // indirecty falls here
401
                                                        address <= address_plus_index;
402
                                                        temp_addr <= {{5{1'b0}}, data_in};
403
                                                end
404 96 creep
                                        end
405 95 creep
                                end
406
                                READ_FROM_POINTER_X: begin
407
                                        pc <= pc;
408
                                        address <= address_plus_index;
409
                                        temp_addr[7:0] <= data_in;
410
                                        control <= MEM_READ;
411
                                end
412
                                READ_FROM_POINTER_X1: begin
413 101 creep
                                        if (jump_indirect) begin
414
                                                pc[12:8] <= data_in[4:0];
415
                                                control <= MEM_READ;
416
                                                address <= {data_in[4:0], pc[7:0]};
417
                                        end
418
                                        else if (indirectx) begin
419
                                                address <= {data_in[4:0], temp_addr[7:0]};
420 96 creep
                                                if (write) begin
421
                                                        control <= MEM_WRITE;
422
                                                        data_out <= alu_result;
423
                                                end
424
                                                else begin
425
                                                        control <= MEM_READ;
426
                                                end
427 95 creep
                                        end
428 96 creep
                                        else begin // indirecty falls here
429
                                                address <= address_plus_index;
430
                                                temp_addr[12:8] <= data_in;
431 95 creep
                                                control <= MEM_READ;
432
                                        end
433
                                end
434 70 creep
                                default: begin
435 95 creep
                                        $write("unknown state"); // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
436 70 creep
                                        $finish(0);
437
                                end
438
 
439
                        endcase
440
                end
441
        end
442
 
443 71 creep
        always @ (*) begin // this is the next_state logic and the output logic always block
444
                alu_opcode = 8'h00;
445
                alu_a = 8'h00;
446
                alu_enable = 1'b0;
447 70 creep
 
448 71 creep
                next_state = RESET; // this prevents the latch
449 68 creep
 
450 71 creep
                case (state)
451
                        RESET: begin
452
                                next_state = FETCH_OP;
453
                        end
454
                        FETCH_OP: begin
455
                                next_state = FETCH_LOW;
456
                        end
457 82 creep
                        //FETCH_OP_CALC: begin // so far no addressing mode required the use of this state
458
                        //      next_state = FETCH_LOW;
459
                        //      alu_opcode = ir;
460
                        //      alu_enable = 1'b1;
461
                        //end
462 71 creep
                        FETCH_OP_CALC_PARAM: begin
463
                                next_state = FETCH_LOW;
464
                                alu_opcode = ir;
465
                                alu_enable = 1'b1;
466
                                alu_a = temp_data;
467
                        end
468
                        FETCH_LOW: begin
469
                                if (accumulator  || implied) begin
470
                                        alu_opcode = ir;
471
                                        alu_enable = 1'b1;
472
                                        next_state = FETCH_OP;
473
                                end
474
                                else if (immediate) begin
475
                                        next_state = FETCH_OP_CALC_PARAM;
476
                                end
477 77 creep
                                else if (zero_page) begin
478
                                        if (read || read_modify_write) begin
479
                                                next_state = READ_MEM;
480
                                        end
481
                                        else if (write) begin
482
                                                next_state = WRITE_MEM;
483 86 creep
                                                alu_opcode = ir;
484
                                                alu_enable = 1'b1;
485
                                                alu_a = 8'h00;
486 77 creep
                                        end
487
                                        else begin
488
                                                $write("unknown behavior");
489
                                                $finish(0);
490
                                        end
491
                                end
492 86 creep
                                else if (zero_page_indexed) begin
493
                                        next_state = READ_MEM_CALC_INDEX;
494
                                end
495 101 creep
                                else if (absolute || jump_indirect) begin // at least the absolute address mode falls here
496 71 creep
                                        next_state = FETCH_HIGH;
497 87 creep
                                        if (write) begin // this is being done one cycle early but i have checked and the ALU will still work properly
498 86 creep
                                                alu_opcode = ir;
499
                                                alu_enable = 1'b1;
500
                                                alu_a = 8'h00;
501
                                        end
502 71 creep
                                end
503 87 creep
                                else if (absolute_indexed) begin
504
                                        next_state = FETCH_HIGH_CALC_INDEX;
505
                                end
506 94 creep
                                else if (relative) begin
507
                                        next_state = FETCH_OP_EVAL_BRANCH;
508
                                end
509 96 creep
                                else if (indirectx || indirecty) begin
510 95 creep
                                        next_state = READ_FROM_POINTER;
511
                                end
512 71 creep
                        end
513 95 creep
                        READ_FROM_POINTER: begin
514 96 creep
                                if (indirectx) begin
515
                                        next_state = READ_FROM_POINTER_X;
516
                                end
517 101 creep
                                else begin // indirecty and jump indirect falls here
518 96 creep
                                        next_state = READ_FROM_POINTER_X1;
519
                                end
520 95 creep
                        end
521
                        READ_FROM_POINTER_X: begin
522
                                next_state = READ_FROM_POINTER_X1;
523
                        end
524
                        READ_FROM_POINTER_X1: begin
525 101 creep
                                if (jump_indirect) begin
526
                                        next_state = FETCH_OP;
527
                                end
528
                                else if (indirecty) begin
529 96 creep
                                        next_state = READ_MEM_FIX_ADDR;
530 95 creep
                                end
531 96 creep
                                else begin
532
                                        if (read || read_modify_write) begin
533
                                                next_state = READ_MEM;
534
                                        end
535
                                        else if (write) begin
536
                                                alu_opcode = ir;
537
                                                alu_enable = 1'b1;
538
                                                next_state = WRITE_MEM;
539
                                        end
540 95 creep
                                end
541
                        end
542 94 creep
                        FETCH_OP_EVAL_BRANCH: begin
543
                                if (branch) begin
544
                                        next_state = FETCH_OP_FIX_PC;
545
                                end
546
                                else begin
547
                                        next_state = FETCH_LOW;
548
                                end
549
                        end
550
                        FETCH_OP_FIX_PC: begin
551
                                if (page_crossed) begin
552
                                        next_state = FETCH_OP;
553
                                end
554
                                else begin
555
                                        next_state = FETCH_LOW;
556
                                end
557
                        end
558 87 creep
                        FETCH_HIGH_CALC_INDEX: begin
559
                                next_state = READ_MEM_FIX_ADDR;
560
                        end
561
                        READ_MEM_FIX_ADDR: begin
562
                                if (read) begin
563
                                        if (page_crossed) begin
564
                                                next_state = READ_MEM;
565
                                        end
566
                                        else begin
567
                                                next_state = FETCH_OP_CALC_PARAM;
568
                                        end
569
                                end
570
                                else if (read_modify_write) begin
571
                                        next_state = READ_MEM;
572
                                end
573
                                else if (write) begin
574
                                        next_state = WRITE_MEM;
575 100 creep
                                        alu_enable = 1'b1;
576
                                        alu_opcode = ir;
577 87 creep
                                end
578
                                else begin
579
                                        $write("unknown behavior");
580
                                        $finish(0);
581
                                end
582
                        end
583 71 creep
                        FETCH_HIGH: begin
584 101 creep
                                if (jump_indirect) begin
585
                                        next_state = READ_FROM_POINTER;
586
                                end
587
                                else if (jump) begin
588 68 creep
                                        next_state = FETCH_OP;
589 61 creep
                                end
590 71 creep
                                else if (read || read_modify_write) begin
591
                                        next_state = READ_MEM;
592 61 creep
                                end
593 71 creep
                                else if (write) begin
594
                                        next_state = WRITE_MEM;
595 68 creep
                                end
596 71 creep
                                else begin
597
                                        $write("unknown behavior");
598
                                        $finish(0);
599 61 creep
                                end
600 71 creep
                        end
601 86 creep
                        READ_MEM_CALC_INDEX: begin
602
                                if (read || read_modify_write) begin
603
                                        next_state = READ_MEM;
604
                                end
605
                                else if (write) begin
606
                                        alu_opcode = ir;
607
                                        alu_enable = 1'b1;
608
                                        next_state = WRITE_MEM;
609
                                end
610
                                else begin
611
                                        $write("unknown behavior");
612
                                        $finish(0);
613
                                end
614
                        end
615 71 creep
                        READ_MEM: begin
616
                                if (read) begin
617
                                        next_state = FETCH_OP_CALC_PARAM;
618 61 creep
                                end
619 71 creep
                                else if (read_modify_write) begin
620
                                        next_state = DUMMY_WRT_CALC;
621
                                end
622
                        end
623
                        DUMMY_WRT_CALC: begin
624
                                alu_opcode = ir;
625
                                alu_enable = 1'b1;
626
                                alu_a = data_in;
627
                                next_state = WRITE_MEM;
628
                        end
629
                        WRITE_MEM: begin
630
                                next_state = FETCH_OP;
631
                        end
632
                        default: begin
633
                                next_state = RESET;
634
                        end
635
                endcase
636 61 creep
        end
637
 
638 77 creep
        // this always block is responsible for updating the address mode and the type of operation being done
639 68 creep
        always @ (*) begin // 
640 61 creep
                absolute = 1'b0;
641
                absolute_indexed = 1'b0;
642
                accumulator = 1'b0;
643
                immediate = 1'b0;
644
                implied = 1'b0;
645 95 creep
                indirectx = 1'b0;
646
                indirecty = 1'b0;
647 61 creep
                relative = 1'b0;
648
                zero_page = 1'b0;
649
                zero_page_indexed = 1'b0;
650 86 creep
 
651
                index = 1'b0;
652 61 creep
 
653
                read = 1'b0;
654
                read_modify_write = 1'b0;
655
                write = 1'b0;
656
                jump = 1'b0;
657 101 creep
                jump_indirect = 1'b0;
658 94 creep
                branch = 1'b0;
659
 
660 70 creep
                case (ir)
661
                        BRK_IMP, CLC_IMP, CLD_IMP, CLI_IMP, CLV_IMP, DEX_IMP, DEY_IMP, INX_IMP, INY_IMP, NOP_IMP, PHA_IMP, PHP_IMP, PLA_IMP,
662
                        PLP_IMP, RTI_IMP, RTS_IMP, SEC_IMP, SED_IMP, SEI_IMP, TAX_IMP, TAY_IMP, TSX_IMP, TXA_IMP, TXS_IMP, TYA_IMP: begin
663
                                implied = 1'b1;
664
                        end
665
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
666
                                accumulator = 1'b1;
667
                        end
668
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
669
                                immediate = 1'b1;
670
                        end
671
                        ADC_ZPG, AND_ZPG, ASL_ZPG, BIT_ZPG, CMP_ZPG, CPX_ZPG, CPY_ZPG, DEC_ZPG, EOR_ZPG, INC_ZPG, LDA_ZPG, LDX_ZPG, LDY_ZPG,
672
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
673
                                zero_page = 1'b1;
674
                        end
675
                        ADC_ZPX, AND_ZPX, ASL_ZPX, CMP_ZPX, DEC_ZPX, EOR_ZPX, INC_ZPX, LDA_ZPX, LDY_ZPX, LSR_ZPX, ORA_ZPX, ROL_ZPX, ROR_ZPX,
676 86 creep
                        SBC_ZPX, STA_ZPX, STY_ZPX: begin
677 70 creep
                                zero_page_indexed = 1'b1;
678 86 creep
                                index = alu_x;
679 70 creep
                        end
680 86 creep
                        LDX_ZPY, STX_ZPY: begin
681
                                zero_page_indexed = 1'b1;
682
                                index = alu_y;
683
                        end
684 94 creep
                        BCC_REL: begin
685 70 creep
                                relative = 1'b1;
686 94 creep
                                index = temp_data;
687
 
688
                                if (!alu_status[C]) begin
689
                                        branch = 1'b1;
690
                                end
691
                                else begin
692
                                        branch = 1'b0;
693
                                end
694 70 creep
                        end
695 94 creep
                        BCS_REL: begin
696
                                relative = 1'b1;
697
                                index = temp_data;
698
 
699
                                if (alu_status[C]) begin
700
                                        branch = 1'b1;
701
                                end
702
                                else begin
703
                                        branch = 1'b0;
704
                                end
705
                        end
706
                        BEQ_REL: begin
707
                                relative = 1'b1;
708
                                index = temp_data;
709
 
710
                                if (alu_status[Z]) begin
711
                                        branch = 1'b1;
712
                                end
713
                                else begin
714
                                        branch = 1'b0;
715
                                end
716
                        end
717
                        BNE_REL: begin
718
                                relative = 1'b1;
719
                                index = temp_data;
720
 
721
                                if (alu_status[Z] == 1'b0) begin
722
                                        branch = 1'b1;
723
                                end
724
                                else begin
725
                                        branch = 1'b0;
726
                                end
727
                        end
728
                        BPL_REL: begin
729
                                relative = 1'b1;
730
                                index = temp_data;
731
 
732
                                if (!alu_status[N]) begin
733
                                        branch = 1'b1;
734
                                end
735
                                else begin
736
                                        branch = 1'b0;
737
                                end
738
                        end
739
                        BMI_REL: begin
740
                                relative = 1'b1;
741
                                index = temp_data;
742
 
743
                                if (alu_status[N]) begin
744
                                        branch = 1'b1;
745
                                end
746
                                else begin
747
                                        branch = 1'b0;
748
                                end
749
                        end
750
                        BVC_REL: begin
751
                                relative = 1'b1;
752
                                index = temp_data;
753
 
754
                                if (!alu_status[V]) begin
755
                                        branch = 1'b1;
756
                                end
757
                                else begin
758
                                        branch = 1'b0;
759
                                end
760
                        end
761
                        BVS_REL: begin
762
                                relative = 1'b1;
763
                                index = temp_data;
764
 
765
                                if (alu_status[V]) begin
766
                                        branch = 1'b1;
767
                                end
768
                                else begin
769
                                        branch = 1'b0;
770
                                end
771
                        end
772 101 creep
                        ADC_ABS, AND_ABS, ASL_ABS, BIT_ABS, CMP_ABS, CPX_ABS, CPY_ABS, DEC_ABS, EOR_ABS, INC_ABS, JSR_ABS, LDA_ABS,
773 70 creep
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
774
                                absolute = 1'b1;
775
                        end
776
                        ADC_ABX, AND_ABX, ASL_ABX, CMP_ABX, DEC_ABX, EOR_ABX, INC_ABX, LDA_ABX, LDY_ABX, LSR_ABX, ORA_ABX, ROL_ABX, ROR_ABX,
777 87 creep
                        SBC_ABX, STA_ABX: begin
778 70 creep
                                absolute_indexed = 1'b1;
779 87 creep
                                index = alu_x;
780 70 creep
                        end
781 87 creep
                        ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
782
                                absolute_indexed = 1'b1;
783
                                index = alu_y;
784
                        end
785 95 creep
                        ADC_IDX, AND_IDX, CMP_IDX, EOR_IDX, LDA_IDX, ORA_IDX, SBC_IDX, STA_IDX: begin
786
                                indirectx = 1'b1;
787
                                index = alu_x;
788 70 creep
                        end
789 95 creep
                        ADC_IDY, AND_IDY, CMP_IDY, EOR_IDY, LDA_IDY, ORA_IDY, SBC_IDY, STA_IDY: begin
790
                                indirecty = 1'b1;
791
                                index = alu_y;
792
                        end
793 101 creep
                        JMP_ABS: begin
794
                                absolute = 1'b1;
795
                                jump = 1'b1;
796
                        end
797
                        JMP_IND: begin
798
                                jump_indirect = 1'b1;
799
                        end
800 71 creep
                        default: begin
801 94 creep
                                $write("state : %b", state);
802
                                if (reset_n == 1 && state != FETCH_OP_FIX_PC) begin // the processor is NOT being reset neither it is fixing the pc
803 86 creep
                                        $write("\nunknown OPCODE!!!!! 0x%h\n", ir);
804
                                        $finish();
805
                                end
806 71 creep
                        end
807 70 creep
                endcase
808 71 creep
 
809
                case (ir)
810
                        ASL_ACC, ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX, LSR_ACC, LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX, ROL_ACC, ROL_ZPG, ROL_ZPX, ROL_ABS,
811
                        ROL_ABX, ROR_ACC, ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX, INC_ZPG, INC_ZPX, INC_ABS, INC_ABX, DEC_ZPG, DEC_ZPX, DEC_ABS,
812
                        DEC_ABX: begin
813
                                read_modify_write = 1'b1;
814
                        end
815
                        STA_ZPG, STA_ZPX, STA_ABS, STA_ABX, STA_ABY, STA_IDX, STA_IDY, STX_ZPG, STX_ZPY, STX_ABS, STY_ZPG, STY_ZPX, STY_ABS: begin
816
                                write = 1'b1;
817
                        end
818
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
819
                                read = 1'b1;
820
                        end
821
                endcase
822 86 creep
        end
823 61 creep
endmodule
824
 
825
 
826
 

powered by: WebSVN 2.1.0

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