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

Subversion Repositories t6507lp

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

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

Line No. Rev Author Line
1 128 gabrielosh
////////////////////////////////////////////////////////////////////////////
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
//// - Fix relative mode, bit 7 means negative                          ////
13
//// - Check reset behavior                                             ////
14
//// - Comment the code                                                 ////
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
`include "timescale.v"
48
 
49
module t6507lp_fsm(clk, reset_n, alu_result, alu_status, data_in, alu_x, alu_y, address, mem_rw, data_out, alu_opcode, alu_a, alu_enable);
50
        parameter [3:0] DATA_SIZE = 4'd8;
51
        parameter [3:0] ADDR_SIZE = 4'd13;
52
 
53
        localparam [3:0] DATA_SIZE_ = DATA_SIZE - 4'b0001;
54
        localparam [3:0] ADDR_SIZE_ = ADDR_SIZE - 4'b0001;
55
 
56
        input clk;                              // master clock
57
        input reset_n;                          // active low reset
58
        input [DATA_SIZE_:0] alu_result; // result from alu operation
59
        input [DATA_SIZE_:0] alu_status; // alu status register
60
        input [DATA_SIZE_:0] data_in;            // data that comes from the bus controller
61
        input [DATA_SIZE_:0] alu_x;              // alu x index register
62
        input [DATA_SIZE_:0] alu_y;              // alu y index register
63
        output reg [ADDR_SIZE_:0] address;       // system bus address
64
        output reg mem_rw;                      // read = 0, write = 1
65
        output reg [DATA_SIZE_:0] data_out;      // data that will be written somewhere else
66
        output reg [DATA_SIZE_:0] alu_opcode;    // current opcode
67
        output reg [DATA_SIZE_:0] alu_a; // extra operand sent to the alu
68
        output reg alu_enable;                  // a flag that when high tells the alu when to perform the operations
69
 
70
 
71
        // FSM states. If aiming for less power consumption try gray coding.
72
        //localparam FETCH_OP_CALC = 5'b00001; this was never used
73
        localparam FETCH_OP = 5'b00000;
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
        localparam PUSH_PCH = 5'b10000;
89
        localparam PUSH_PCL = 5'b10001;
90
        localparam PUSH_STATUS = 5'b10010;
91
        localparam FETCH_PCL = 5'b10011;
92
        localparam FETCH_PCH = 5'b10100;
93
        localparam INCREMENT_SP = 5'b10101;
94
        localparam PULL_STATUS = 5'b10110;
95
        localparam PULL_PCL = 5'b10111;
96
        localparam PULL_PCH = 5'b11000;
97
        localparam INCREMENT_PC = 5'b11001;
98
        localparam PUSH_REGISTER = 5'b11010;
99
        localparam PULL_REGISTER = 5'b11011;
100
        localparam DUMMY = 5'b11100;
101
        localparam RESET = 5'b11111;
102
 
103
        // OPCODES TODO: verify how this get synthesised
104 146 creep
        `include "t6507lp_package.v"
105 128 gabrielosh
 
106
        // mem_rw signals
107
        localparam MEM_READ = 1'b0;
108
        localparam MEM_WRITE = 1'b1;
109
 
110
        reg [ADDR_SIZE_:0] pc;           // program counter
111
        reg [DATA_SIZE:0] sp;            // stack pointer. 9 bits wide.
112
        reg [DATA_SIZE_:0] ir;           // instruction register
113
        reg [ADDR_SIZE_:0] temp_addr;    // temporary address
114
        reg [DATA_SIZE_:0] temp_data;    // temporary data
115
 
116
        reg [4:0] state, next_state; // current and next state registers
117
 
118
        // wiring that simplifies the FSM logic by simplifying the addressing modes
119
        reg absolute;
120
        reg absolute_indexed;
121
        reg accumulator;
122
        reg immediate;
123
        reg implied;
124
        reg indirectx;
125
        reg indirecty;
126
        reg relative;
127
        reg zero_page;
128
        reg zero_page_indexed;
129
        reg [DATA_SIZE_:0] index; // will be assigned with either X or Y
130
 
131
        // regs that store the type of operation. again, this simplifies the FSM a lot.
132
        reg read;
133
        reg read_modify_write;
134
        reg write;
135
        reg jump;
136
        reg jump_indirect;
137
 
138
        // regs for the special instructions
139
        reg brk;
140
        reg rti;
141
        reg rts;
142
        reg pha;
143
        reg php;
144
        reg pla;
145
        reg plp;
146 194 creep
        reg jsr;
147
        reg tsx;
148
        reg txs;
149 128 gabrielosh
 
150
        wire [ADDR_SIZE_:0] next_pc;      // a simple logic to add one to the PC
151
        assign next_pc = pc + 13'b0000000000001;
152
 
153
        wire [8:0] sp_plus_one;          // simple adder and subtracter for the stack pointer
154
        assign sp_plus_one = sp + 9'b000000001;
155
 
156
        wire [8:0] sp_minus_one;
157
        assign sp_minus_one = sp - 9'b000000001;
158
 
159
        reg [ADDR_SIZE_:0] address_plus_index;   // this two registers are used when the instruction uses indexing.
160
        reg page_crossed;                       // address_plus_index always adds index to address and page_crossed asserts when the sum creates a carry.
161
 
162
        reg branch;     // a simple reg that is asserted everytime a branch will be executed.                   
163
 
164
        // this is the combinational logic related to indexed instructions
165
        always @(*) begin
166
                address_plus_index = 8'h00;
167
                page_crossed = 1'b0;
168
 
169
                if (state == READ_MEM_CALC_INDEX || state == READ_MEM_FIX_ADDR || state == FETCH_HIGH_CALC_INDEX) begin
170
                        {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
171
                        address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
172
                end
173
                else if (branch) begin
174
                        if (state == FETCH_OP_FIX_PC || state == FETCH_OP_EVAL_BRANCH) begin
175
                                {page_crossed, address_plus_index[7:0]} = pc[7:0] + index;
176
                                address_plus_index[12:8] = pc[12:8] + page_crossed;     // warning: pc might feed these lines twice and cause branch failure
177
                        end                                                             // solution: add a temp reg i guess
178
                end
179
                else if (state == READ_FROM_POINTER) begin
180
                        if (indirectx) begin
181
                                {page_crossed, address_plus_index[7:0]} = temp_data + index;
182
                                address_plus_index[12:8] = 5'b00000;
183
                        end
184
                        else if (jump_indirect) begin
185
                                address_plus_index[7:0] = temp_addr[7:0] + 8'h01; // temp_addr should be 7:0?
186
                                address_plus_index[12:8] = 5'b00000;
187
                        end
188
                        else begin // indirecty falls here
189
                                address_plus_index[7:0] = temp_data + 8'h01;
190
                                address_plus_index[12:8] = 5'b00000;
191
                        end
192
                end
193
                else if (state == READ_FROM_POINTER_X) begin
194
                        {page_crossed, address_plus_index[7:0]} = temp_data + index + 8'h01;
195
                        address_plus_index[12:8] = 5'b00000;
196
                end
197
                else if (state == READ_FROM_POINTER_X1) begin
198
                        {page_crossed, address_plus_index[7:0]} = temp_addr[7:0] + index;
199
                        address_plus_index[12:8] = temp_addr[12:8] + page_crossed;
200
                end
201
        end
202
 
203 146 creep
        reg [2:0] rst_counter; // a counter to preserve the cpu idle for six cycles
204
 
205 128 gabrielosh
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
206
                if (reset_n == 1'b0) begin
207
                        // all registers must assume default values
208
                        pc <= 0; // TODO: this is written somewhere. something about a reset vector. must be checked.
209 194 creep
                        sp <= 9'b111111111; // the default is 'h1FF 
210 128 gabrielosh
                        ir <= 8'h00;
211
                        temp_addr <= 13'h0000;
212
                        temp_data <= 8'h00;
213
                        state <= RESET;
214
                        // registered outputs also receive default values
215
                        address <= 13'h0000;
216
                        mem_rw <= MEM_READ;
217
                        data_out <= 8'h00;
218 146 creep
                        rst_counter <= 0;
219 128 gabrielosh
                end
220
                else begin
221
                        state <= next_state;
222 146 creep
 
223 128 gabrielosh
                        case (state)
224
                                RESET: begin    // The processor was reset
225 146 creep
                                        rst_counter <= rst_counter + 1;
226 194 creep
                                        //sp <= 9'b111111111; // this prevents flipflops with different drivers
227 128 gabrielosh
                                        //$write("under reset"); 
228
                                end
229
                                /*
230
                                FETCH_OP: executed when the processor was reset or the last instruction could not fetch.
231
                                FETCH_OP_CALC_PARAM: enables the alu with an argument (alu_a) and fetchs the next instruction opcode. (pipelining)
232
                                */
233
                                FETCH_OP, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
234
                                        pc <= next_pc;
235
                                        address <= next_pc;
236
                                        mem_rw <= MEM_READ;
237
                                        ir <= data_in;
238
                                end
239
                                /*
240
                                in this state the opcode is already known so truly execution begins.
241
                                all instructions execute this cycle.
242
                                */
243 196 creep
                                FETCH_LOW: begin
244 195 creep
                                        if (accumulator || implied || txs || tsx) begin
245 128 gabrielosh
                                                pc <= pc; // is this better?
246
                                                address <= pc;
247 194 creep
                                                mem_rw <= MEM_READ;
248
 
249
                                                if (txs) begin
250 196 creep
                                                        sp[7:0] <= alu_x;
251 194 creep
                                                end
252
                                                //alu_a
253 128 gabrielosh
                                        end
254
                                        else if (immediate || relative) begin
255
                                                pc <= next_pc;
256
                                                address <= next_pc;
257
                                                mem_rw <= MEM_READ;
258
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
259
                                        end
260
                                        else if (absolute || absolute_indexed || jump_indirect) begin
261
                                                pc <= next_pc;
262 196 creep
                                                address <= next_pc;
263 128 gabrielosh
                                                mem_rw <= MEM_READ;
264
                                                temp_addr <= {{5{1'b0}},data_in};
265
                                                temp_data <= 8'h00;
266
                                        end
267
                                        else if (zero_page) begin
268
                                                pc <= next_pc;
269
                                                address <= {{5{1'b0}},data_in};
270
                                                temp_addr <= {{5{1'b0}},data_in};
271
 
272
                                                if (write) begin
273
                                                        mem_rw <= MEM_WRITE;
274
                                                        data_out <= alu_result;
275
                                                end
276
                                                else begin
277
                                                        mem_rw <= MEM_READ;
278
                                                        data_out <= 8'h00;
279
                                                end
280
                                        end
281
                                        else if (zero_page_indexed) begin
282
                                                pc <= next_pc;
283
                                                address <= {{5{1'b0}}, data_in};
284
                                                temp_addr <= {{5{1'b0}}, data_in};
285
                                                mem_rw <= MEM_READ;
286
                                        end
287
                                        else if (indirectx || indirecty) begin
288
                                                pc <= next_pc;
289
                                                address <= data_in;
290
                                                temp_data <= data_in;
291
                                                mem_rw <= MEM_READ;
292
                                        end
293
                                        else begin // the special instructions will fall here: BRK, RTI, RTS...
294
                                                if (brk) begin
295
                                                        pc <= next_pc;
296
                                                        address <= sp;
297
                                                        data_out <= {{3{1'b0}}, pc[12:8]};
298
                                                        mem_rw <= MEM_WRITE;
299
                                                end
300
                                                else if (rti || rts) begin
301
                                                        address <= sp;
302
                                                        mem_rw <= MEM_READ;
303
                                                end
304
                                                else if (pha || php) begin
305
                                                        pc <= pc;
306
                                                        address <= sp;
307
                                                        data_out <= (pha) ? alu_result : alu_status;
308
                                                        mem_rw <= MEM_WRITE;
309
                                                end
310
                                                else if (pla || plp) begin
311
                                                        pc <= pc;
312
                                                        address <= sp;
313
                                                        mem_rw <= MEM_READ;
314
                                                end
315
                                                else begin // jsr
316
                                                        address <= sp;
317
                                                        mem_rw <= MEM_READ;
318
                                                        temp_addr <= {{5{1'b0}}, data_in};
319
                                                        pc <= next_pc;
320
                                                end
321
                                        end
322
                                end
323
                                FETCH_HIGH_CALC_INDEX: begin
324
                                        pc <= next_pc;
325
                                        temp_addr[12:8] <= data_in[4:0];
326
                                        address <= {data_in[4:0], address_plus_index[7:0]};
327
                                        mem_rw <= MEM_READ;
328
                                        data_out <= 8'h00;
329
                                end
330
                                // this cycle fetchs the next operand while still evaluating if a branch occurred.
331
                                FETCH_OP_EVAL_BRANCH: begin
332
                                        if (branch) begin
333
                                                pc <= {{5{1'b0}}, address_plus_index[7:0]};
334
                                                address <= {{5{1'b0}}, address_plus_index[7:0]};
335
                                                mem_rw <= MEM_READ;
336
                                                data_out <= 8'h00;
337
                                        end
338
                                        else begin
339
                                                pc <= next_pc;
340
                                                address <= next_pc;
341
                                                mem_rw <= MEM_READ;
342
                                                data_out <= 8'h00;
343
                                                ir <= data_in;
344
                                        end
345
                                end
346
                                // sometimes when reading memory page crosses may occur. the pc register must be fixed, i.e., add 16'h0100
347
                                FETCH_OP_FIX_PC: begin
348
                                        if (page_crossed) begin
349
                                                pc[12:8] <= address_plus_index[12:8];
350
                                                address[12:8] <= address_plus_index[12:8];
351
                                        end
352
                                        else begin
353
                                                pc <= next_pc;
354
                                                address <= next_pc;
355
                                                mem_rw <= MEM_READ;
356
                                                ir <= data_in;
357
                                        end
358
                                end
359
                                // several instructions ocupy 3 bytes in memory. this cycle reads the third byte.
360
                                FETCH_HIGH: begin
361
                                        if (jump) begin
362
                                                pc <= {data_in[4:0], temp_addr[7:0]}; // PCL <= first byte, PCH <= second byte
363
                                                address <= {data_in[4:0], temp_addr[7:0]};
364
                                                mem_rw <= MEM_READ;
365
                                                data_out <= 8'h00;
366
                                        end
367
                                        else begin
368
                                                if (write) begin
369
                                                        pc <= next_pc;
370
                                                        temp_addr[12:8] <= data_in[4:0];
371
                                                        address <= {data_in[4:0],temp_addr[7:0]};
372
                                                        mem_rw <= MEM_WRITE;
373
                                                        data_out <= alu_result;
374
                                                end
375
                                                else begin // read_modify_write or just read
376
                                                        pc <= next_pc;
377
                                                        temp_addr[12:8] <= data_in[4:0];
378
                                                        address <= {data_in[4:0],temp_addr[7:0]};
379
                                                        mem_rw <= MEM_READ;
380
                                                        data_out <= 8'h00;
381
                                                end
382
                                        end
383
                                end
384
                                // read memory at address
385
                                READ_MEM: begin
386
                                        if (read_modify_write) begin
387
                                                pc <= pc;
388
                                                address <= temp_addr;
389
                                                mem_rw <= MEM_WRITE;
390
                                                temp_data <= data_in;
391
                                                data_out <= data_in; // writeback the same value
392
                                        end
393
                                        else begin
394
                                                pc <= pc;
395
                                                address <= pc;
396
                                                temp_data <= data_in;
397
                                                mem_rw <= MEM_READ;
398
                                                data_out <= 8'h00;
399
                                        end
400
                                end
401
                                READ_MEM_CALC_INDEX: begin
402
                                                address <= address_plus_index;
403
                                                temp_addr <= address_plus_index;
404
 
405
                                                if (write) begin
406
                                                        mem_rw <= MEM_WRITE;
407
                                                        data_out <= alu_result;
408
                                                end
409
                                                else begin
410
                                                        mem_rw <= MEM_READ;
411
                                                        data_out <= 8'h00;
412
                                                end
413
 
414
                                end
415
                                READ_MEM_FIX_ADDR: begin
416
                                        if (read) begin
417
                                                mem_rw <= MEM_READ;
418
                                                data_out <= 8'h00;
419
 
420
                                                if (page_crossed) begin // fix address 
421
                                                        address <= address_plus_index;
422
                                                        temp_addr <= address_plus_index;
423
                                                end
424
                                                else begin
425
                                                        address <= pc;
426
                                                        temp_data <= data_in;
427
                                                end
428
                                        end
429
                                        else if (write) begin
430
                                                mem_rw <= MEM_WRITE;
431
                                                data_out <= alu_result;
432
                                                address <= address_plus_index;
433
                                                temp_addr <= address_plus_index;
434
 
435
                                        end
436
                                        else begin // read modify write
437
                                                mem_rw <= MEM_READ;
438
                                                data_out <= 8'h00;
439
                                                address <= address_plus_index;
440
                                                temp_addr <= address_plus_index;
441
                                        end
442
                                end
443
                                // some instructions have a dummy write cycle. this is it.
444
                                DUMMY_WRT_CALC: begin
445
                                        pc <= pc;
446
                                        address <= temp_addr;
447
                                        mem_rw <= MEM_WRITE;
448
                                        data_out <= alu_result;
449
                                end
450
                                WRITE_MEM: begin
451
                                        pc <= pc;
452
                                        address <= pc;
453
                                        mem_rw <= MEM_READ;
454
                                        data_out <= 8'h00;
455
                                end
456
                                READ_FROM_POINTER: begin
457
                                        if (jump_indirect) begin
458
                                                pc[7:0] <= data_in;
459
                                                mem_rw <= MEM_READ;
460
                                                address <= address_plus_index;
461
                                        end
462
                                        else begin
463
                                                pc <= pc;
464
                                                mem_rw <= MEM_READ;
465
 
466
                                                if (indirectx) begin
467
                                                        address <= address_plus_index;
468
                                                end
469
                                                else begin // indirecty falls here
470
                                                        address <= address_plus_index;
471
                                                        temp_addr <= {{5{1'b0}}, data_in};
472
                                                end
473
                                        end
474
                                end
475
                                READ_FROM_POINTER_X: begin
476
                                        pc <= pc;
477
                                        address <= address_plus_index;
478
                                        temp_addr[7:0] <= data_in;
479
                                        mem_rw <= MEM_READ;
480
                                end
481
                                READ_FROM_POINTER_X1: begin
482
                                        if (jump_indirect) begin
483
                                                pc[12:8] <= data_in[4:0];
484
                                                mem_rw <= MEM_READ;
485
                                                address <= {data_in[4:0], pc[7:0]};
486
                                        end
487
                                        else if (indirectx) begin
488
                                                address <= {data_in[4:0], temp_addr[7:0]};
489
                                                if (write) begin
490
                                                        mem_rw <= MEM_WRITE;
491
                                                        data_out <= alu_result;
492
                                                end
493
                                                else begin
494
                                                        mem_rw <= MEM_READ;
495
                                                end
496
                                        end
497
                                        else begin // indirecty falls here
498
                                                address <= address_plus_index;
499
                                                temp_addr[12:8] <= data_in;
500
                                                mem_rw <= MEM_READ;
501
                                        end
502
                                end
503
                                PUSH_PCH: begin
504
                                        pc <= pc;
505
                                        address <= sp_minus_one;
506
                                        data_out <= pc[7:0];
507
                                        mem_rw <= MEM_WRITE;
508
                                        sp <= sp_minus_one;
509
                                end
510
                                PUSH_PCL: begin
511
                                        if (jsr) begin
512
                                                pc <= pc;
513
                                                address <= pc;
514
                                                mem_rw <= MEM_READ;
515
                                                sp <= sp_minus_one;
516
                                        end
517
                                        else begin
518
                                                pc <= pc;
519
                                                address <= sp_minus_one;
520
                                                data_out <= alu_status;
521
                                                mem_rw <= MEM_WRITE;
522
                                                sp <= sp_minus_one;
523
                                        end
524
                                end
525
                                PUSH_STATUS: begin
526
                                        address <= 13'hFFFE;
527
                                        mem_rw <= MEM_READ;
528 196 creep
                                        sp <= sp_minus_one;
529 128 gabrielosh
                                end
530
                                FETCH_PCL: begin
531
                                        pc[7:0] <= data_in;
532
                                        address <= 13'hFFFF;
533
                                        mem_rw <= MEM_READ;
534
                                end
535
                                FETCH_PCH: begin
536
                                        pc[12:8] <= data_in[4:0];
537
                                        address <= {data_in[4:0], pc[7:0]};
538
                                        mem_rw <= MEM_READ;
539
                                end
540
                                INCREMENT_SP: begin
541
                                        sp <= sp_plus_one;
542
                                        address <= sp_plus_one;
543
                                end
544
                                PULL_STATUS: begin
545
                                        sp <= sp_plus_one;
546
                                        address <= sp_plus_one;
547
                                        temp_data <= data_in;
548
                                end
549
                                PULL_PCL: begin
550
                                        sp <= sp_plus_one;
551
                                        address <= sp_plus_one;
552
                                        pc[7:0] <= data_in;
553
                                end
554
                                PULL_PCH: begin
555
                                        pc[12:8] <= data_in[4:0];
556
                                        address <= {data_in[4:0], pc[7:0]};
557
                                end
558
                                INCREMENT_PC: begin
559
                                        pc <= next_pc;
560
                                        address <= next_pc;
561
                                end
562
                                PUSH_REGISTER: begin
563
                                        pc <= pc;
564
                                        address <= pc;
565
                                        sp <= sp_minus_one;
566
                                        mem_rw <= MEM_READ;
567
                                        temp_data <= data_in;
568
                                end
569
                                PULL_REGISTER: begin
570
                                        pc <= pc;
571
                                        address <= pc;
572
                                        temp_data <= data_in;
573
                                end
574
                                DUMMY: begin
575
                                        address <= sp;
576
                                        mem_rw <= MEM_WRITE;
577
                                end
578
                                default: begin
579
                                        //$write("unknown state"); // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
580
                                        //$finish(0); 
581
                                end
582
 
583
                        endcase
584
                end
585
        end
586
 
587
        always @ (*) begin // this is the next_state logic and the combinational output logic always block
588
                alu_opcode = 8'h00;
589
                alu_a = 8'h00;
590
                alu_enable = 1'b0;
591
                next_state = RESET; // these lines prevents latches
592
 
593
                case (state)
594
                        RESET: begin
595 146 creep
                                if (rst_counter == 6) begin
596
                                        next_state = FETCH_OP;
597
                                end
598 128 gabrielosh
                        end
599
                        FETCH_OP: begin
600
                                next_state = FETCH_LOW;
601
                        end
602
                        FETCH_OP_CALC_PARAM: begin
603
                                next_state = FETCH_LOW;
604
                                alu_opcode = ir;
605
                                alu_enable = 1'b1;
606
                                alu_a = temp_data;
607
                        end
608
                        FETCH_LOW: begin
609 195 creep
                                if (accumulator  || implied || txs) begin
610 128 gabrielosh
                                        alu_opcode = ir;
611
                                        alu_enable = 1'b1;
612 194 creep
                                        next_state = FETCH_OP;
613 128 gabrielosh
                                end
614 195 creep
                                else if (tsx) begin
615
                                        alu_opcode = ir;
616
                                        alu_enable = 1'b1;
617
                                        next_state = FETCH_OP;
618
                                        alu_a = sp[7:0];
619
                                end
620 128 gabrielosh
                                else if (immediate) begin
621
                                        next_state = FETCH_OP_CALC_PARAM;
622
                                end
623
                                else if (zero_page) begin
624
                                        if (read || read_modify_write) begin
625
                                                next_state = READ_MEM;
626
                                        end
627
                                        else if (write) begin
628
                                                next_state = WRITE_MEM;
629
                                                alu_opcode = ir;
630
                                                alu_enable = 1'b1;
631
                                                alu_a = 8'h00;
632
                                        end
633
                                        else begin
634
                                                //$write("unknown behavior"); 
635
                                                //$finish(0);
636
                                        end
637
                                end
638
                                else if (zero_page_indexed) begin
639
                                        next_state = READ_MEM_CALC_INDEX;
640
                                end
641
                                else if (absolute || jump_indirect) begin
642
                                        next_state = FETCH_HIGH;
643
                                        if (write) begin // this is being done one cycle early but i have checked and the ALU will still work properly
644
                                                alu_opcode = ir;
645
                                                alu_enable = 1'b1;
646
                                                alu_a = 8'h00;
647
                                        end
648
                                end
649
                                else if (absolute_indexed) begin
650
                                        next_state = FETCH_HIGH_CALC_INDEX;
651
                                end
652
                                else if (relative) begin
653
                                        next_state = FETCH_OP_EVAL_BRANCH;
654
                                end
655
                                else if (indirectx || indirecty) begin
656
                                        next_state = READ_FROM_POINTER;
657
                                end
658
                                else begin // all the special instructions will fall here
659
                                        if (brk) begin
660
                                                next_state = PUSH_PCH;
661
                                        end
662
                                        else if (rti || rts) begin
663
                                                next_state = INCREMENT_SP;
664
                                        end
665
                                        else if (pha) begin
666
                                                alu_opcode = ir;
667
                                                alu_enable = 1'b1;
668
                                                //alu_a = 8'h00;
669
                                                next_state = PUSH_REGISTER;
670
                                        end
671
                                        else if (php) begin
672
                                                next_state = PUSH_REGISTER;
673
                                        end
674
                                        else if (pla || plp) begin
675
                                                next_state = INCREMENT_SP;
676
                                        end
677
                                        else begin // jsr
678
                                                next_state = DUMMY;
679
                                        end
680
                                end
681
                        end
682
                        READ_FROM_POINTER: begin
683
                                if (indirectx) begin
684
                                        next_state = READ_FROM_POINTER_X;
685
                                end
686
                                else begin // indirecty and jump indirect falls here
687
                                        next_state = READ_FROM_POINTER_X1;
688
                                end
689
                        end
690
                        READ_FROM_POINTER_X: begin
691
                                next_state = READ_FROM_POINTER_X1;
692
                        end
693
                        READ_FROM_POINTER_X1: begin
694
                                if (jump_indirect) begin
695
                                        next_state = FETCH_OP;
696
                                end
697
                                else if (indirecty) begin
698
                                        next_state = READ_MEM_FIX_ADDR;
699
                                end
700
                                else begin
701
                                        if (read) begin // no instruction using pointers is from type read_modify_write
702
                                                next_state = READ_MEM;
703
                                        end
704
                                        else if (write) begin
705
                                                alu_opcode = ir;
706
                                                alu_enable = 1'b1;
707
                                                next_state = WRITE_MEM;
708
                                        end
709
                                end
710
                        end
711
                        FETCH_OP_EVAL_BRANCH: begin
712
                                if (branch) begin
713
                                        next_state = FETCH_OP_FIX_PC;
714
                                end
715
                                else begin
716
                                        next_state = FETCH_LOW;
717
                                end
718
                        end
719
                        FETCH_OP_FIX_PC: begin
720
                                if (page_crossed) begin
721
                                        next_state = FETCH_OP;
722
                                end
723
                                else begin
724
                                        next_state = FETCH_LOW;
725
                                end
726
                        end
727
                        FETCH_HIGH_CALC_INDEX: begin
728
                                next_state = READ_MEM_FIX_ADDR;
729
                        end
730
                        READ_MEM_FIX_ADDR: begin
731
                                if (read) begin
732
                                        if (page_crossed) begin
733
                                                next_state = READ_MEM;
734
                                        end
735
                                        else begin
736
                                                next_state = FETCH_OP_CALC_PARAM;
737
                                        end
738
                                end
739
                                else if (read_modify_write) begin
740
                                        next_state = READ_MEM;
741
                                end
742
                                else if (write) begin
743
                                        next_state = WRITE_MEM;
744
                                        alu_enable = 1'b1;
745
                                        alu_opcode = ir;
746
                                end
747
                                else begin
748
                                        //$write("unknown behavior"); 
749
                                        //$finish(0);
750
                                end
751
                        end
752
                        FETCH_HIGH: begin
753
                                if (jump_indirect) begin
754
                                        next_state = READ_FROM_POINTER;
755
                                end
756
                                else if (jump) begin
757
                                        next_state = FETCH_OP;
758
                                end
759
                                else if (read || read_modify_write) begin
760
                                        next_state = READ_MEM;
761
                                end
762
                                else if (write) begin
763
                                        next_state = WRITE_MEM;
764
                                end
765
                                else begin
766
                                        //$write("unknown behavior"); 
767
                                        //$finish(0);
768
                                end
769
                        end
770
                        READ_MEM_CALC_INDEX: begin
771
                                if (read || read_modify_write) begin
772
                                        next_state = READ_MEM;
773
                                end
774
                                else if (write) begin
775
                                        alu_opcode = ir;
776
                                        alu_enable = 1'b1;
777
                                        next_state = WRITE_MEM;
778
                                end
779
                                else begin
780
                                        //$write("unknown behavior"); 
781
                                        //$finish(0);
782
                                end
783
                        end
784
                        READ_MEM: begin
785
                                if (read) begin
786
                                        next_state = FETCH_OP_CALC_PARAM;
787
                                end
788
                                else if (read_modify_write) begin
789
                                        next_state = DUMMY_WRT_CALC;
790
                                end
791
                        end
792
                        DUMMY_WRT_CALC: begin
793
                                alu_opcode = ir;
794
                                alu_enable = 1'b1;
795
                                alu_a = data_in;
796
                                next_state = WRITE_MEM;
797
                        end
798
                        WRITE_MEM: begin
799
                                next_state = FETCH_OP;
800
                        end
801
                        PUSH_PCH: begin
802
                                next_state = PUSH_PCL;
803
                        end
804
                        PUSH_PCL: begin
805
                                if (jsr) begin
806
                                        next_state = FETCH_HIGH;
807
                                end
808
                                else begin
809
                                        next_state = PUSH_STATUS;
810
                                end
811
                        end
812
                        PUSH_STATUS: begin
813
                                next_state = FETCH_PCL;
814
                        end
815
                        FETCH_PCL: begin
816
                                next_state = FETCH_PCH;
817
                        end
818
                        FETCH_PCH: begin
819
                                next_state = FETCH_OP;
820
                        end
821
                        INCREMENT_SP: begin
822
                                if (rti) begin
823
                                        next_state = PULL_STATUS;
824
                                end
825
                                else if (pla || plp) begin
826
                                        next_state = PULL_REGISTER;
827
                                end
828
                                else begin // rts
829
                                        next_state = PULL_PCL;
830
                                end
831
                        end
832
                        PULL_STATUS: begin
833
                                next_state = PULL_PCL;
834
                        end
835
                        PULL_PCL: begin
836
                                next_state = PULL_PCH;
837
                                alu_opcode = ir;
838
                                alu_enable = 1'b1;
839
                                alu_a = temp_data;
840
                        end
841
                        PULL_PCH: begin
842
                                if (rti) begin
843
                                        next_state = FETCH_OP;
844
                                end
845
                                else begin // rts
846
                                        next_state = INCREMENT_PC;
847
                                end
848
                        end
849
                        INCREMENT_PC: begin
850
                                next_state = FETCH_OP;
851
                        end
852
                        PUSH_REGISTER: begin
853
                                next_state = FETCH_OP;
854
                        end
855
                        PULL_REGISTER: begin
856
                                next_state = FETCH_OP_CALC_PARAM;
857
                        end
858
                        DUMMY: begin
859
                                next_state = PUSH_PCH;
860
                        end
861
                        default: begin
862
                                next_state = RESET;
863
                        end
864
                endcase
865
        end
866
 
867
        // this always block is responsible for updating the address mode and the type of operation being done
868
        always @ (*) begin // 
869
                absolute = 1'b0;
870
                absolute_indexed = 1'b0;
871
                accumulator = 1'b0;
872
                immediate = 1'b0;
873
                implied = 1'b0;
874
                indirectx = 1'b0;
875
                indirecty = 1'b0;
876
                relative = 1'b0;
877
                zero_page = 1'b0;
878
                zero_page_indexed = 1'b0;
879
 
880
                index = 8'h00;
881
 
882
                read = 1'b0;
883
                read_modify_write = 1'b0;
884
                write = 1'b0;
885
                jump = 1'b0;
886
                jump_indirect = 1'b0;
887
                branch = 1'b0;
888
 
889
                brk = 1'b0;
890
                rti = 1'b0;
891
                rts = 1'b0;
892
                pha = 1'b0;
893
                php = 1'b0;
894
                pla = 1'b0;
895
                plp = 1'b0;
896
                jsr = 1'b0;
897 194 creep
                tsx = 1'b0;
898
                txs = 1'b0;
899 128 gabrielosh
 
900
                case (ir)
901
                        CLC_IMP, CLD_IMP, CLI_IMP, CLV_IMP, DEX_IMP, DEY_IMP, INX_IMP, INY_IMP, NOP_IMP,
902 194 creep
                        SEC_IMP, SED_IMP, SEI_IMP, TAX_IMP, TAY_IMP, TXA_IMP, TYA_IMP: begin
903 128 gabrielosh
                                implied = 1'b1;
904
                        end
905
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
906
                                accumulator = 1'b1;
907
                        end
908
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
909
                                immediate = 1'b1;
910
                        end
911
                        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,
912
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
913
                                zero_page = 1'b1;
914
                        end
915
                        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,
916
                        SBC_ZPX, STA_ZPX, STY_ZPX: begin
917
                                zero_page_indexed = 1'b1;
918
                                index = alu_x;
919
                        end
920
                        LDX_ZPY, STX_ZPY: begin
921
                                zero_page_indexed = 1'b1;
922
                                index = alu_y;
923
                        end
924
                        BCC_REL: begin
925
                                relative = 1'b1;
926
                                index = temp_data;
927
 
928
                                if (!alu_status[C]) begin
929
                                        branch = 1'b1;
930
                                end
931
                                else begin
932
                                        branch = 1'b0;
933
                                end
934
                        end
935
                        BCS_REL: begin
936
                                relative = 1'b1;
937
                                index = temp_data;
938
 
939
                                if (alu_status[C]) begin
940
                                        branch = 1'b1;
941
                                end
942
                                else begin
943
                                        branch = 1'b0;
944
                                end
945
                        end
946
                        BEQ_REL: begin
947
                                relative = 1'b1;
948
                                index = temp_data;
949
 
950
                                if (alu_status[Z]) begin
951
                                        branch = 1'b1;
952
                                end
953
                                else begin
954
                                        branch = 1'b0;
955
                                end
956
                        end
957
                        BNE_REL: begin
958
                                relative = 1'b1;
959
                                index = temp_data;
960
 
961
                                if (alu_status[Z] == 1'b0) begin
962
                                        branch = 1'b1;
963
                                end
964
                                else begin
965
                                        branch = 1'b0;
966
                                end
967
                        end
968
                        BPL_REL: begin
969
                                relative = 1'b1;
970
                                index = temp_data;
971
 
972
                                if (!alu_status[N]) begin
973
                                        branch = 1'b1;
974
                                end
975
                                else begin
976
                                        branch = 1'b0;
977
                                end
978
                        end
979
                        BMI_REL: begin
980
                                relative = 1'b1;
981
                                index = temp_data;
982
 
983
                                if (alu_status[N]) begin
984
                                        branch = 1'b1;
985
                                end
986
                                else begin
987
                                        branch = 1'b0;
988
                                end
989
                        end
990
                        BVC_REL: begin
991
                                relative = 1'b1;
992
                                index = temp_data;
993
 
994
                                if (!alu_status[V]) begin
995
                                        branch = 1'b1;
996
                                end
997
                                else begin
998
                                        branch = 1'b0;
999
                                end
1000
                        end
1001
                        BVS_REL: begin
1002
                                relative = 1'b1;
1003
                                index = temp_data;
1004
 
1005
                                if (alu_status[V]) begin
1006
                                        branch = 1'b1;
1007
                                end
1008
                                else begin
1009
                                        branch = 1'b0;
1010
                                end
1011
                        end
1012
                        ADC_ABS, AND_ABS, ASL_ABS, BIT_ABS, CMP_ABS, CPX_ABS, CPY_ABS, DEC_ABS, EOR_ABS, INC_ABS, LDA_ABS,
1013
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
1014
                                absolute = 1'b1;
1015
                        end
1016
                        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,
1017
                        SBC_ABX, STA_ABX: begin
1018
                                absolute_indexed = 1'b1;
1019
                                index = alu_x;
1020
                        end
1021
                        ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
1022
                                absolute_indexed = 1'b1;
1023
                                index = alu_y;
1024
                        end
1025
                        ADC_IDX, AND_IDX, CMP_IDX, EOR_IDX, LDA_IDX, ORA_IDX, SBC_IDX, STA_IDX: begin
1026
                                indirectx = 1'b1;
1027
                                index = alu_x;
1028
                        end
1029
                        ADC_IDY, AND_IDY, CMP_IDY, EOR_IDY, LDA_IDY, ORA_IDY, SBC_IDY, STA_IDY: begin
1030
                                indirecty = 1'b1;
1031
                                index = alu_y;
1032
                        end
1033
                        JMP_ABS: begin
1034
                                absolute = 1'b1;
1035
                                jump = 1'b1;
1036
                        end
1037
                        JMP_IND: begin
1038
                                jump_indirect = 1'b1;
1039
                        end
1040
                        BRK_IMP: begin
1041
                                brk = 1'b1;
1042
                        end
1043
                        RTI_IMP: begin
1044
                                rti = 1'b1;
1045
                        end
1046
                        RTS_IMP: begin
1047
                                rts = 1'b1;
1048
                        end
1049
                        PHA_IMP: begin
1050
                                pha = 1'b1;
1051
                        end
1052
                        PHP_IMP: begin
1053
                                php = 1'b1;
1054
                        end
1055
                        PLA_IMP: begin
1056
                                pla = 1'b1;
1057
                        end
1058
                        PLP_IMP: begin
1059
                                plp = 1'b1;
1060
                        end
1061
                        JSR_ABS: begin
1062
                                jsr = 1'b1;
1063
                        end
1064 194 creep
                        TSX_IMP: begin
1065
                                tsx = 1'b1;
1066
                        end
1067
                        TXS_IMP: begin
1068
                                txs = 1'b1;
1069
                        end
1070 128 gabrielosh
                        default: begin
1071
                                //$write("state : %b", state);
1072
                                if (reset_n == 1'b1 && state != FETCH_OP_FIX_PC) begin // the processor is NOT being reset neither it is fixing the pc
1073
                                        //$write("\nunknown OPCODE!!!!! 0x%h\n", ir);
1074
                                        //$finish();
1075
                                end
1076
                        end
1077
                endcase
1078
 
1079
                case (ir)
1080
                        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,
1081
                        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,
1082
                        DEC_ABX: begin
1083
                                read_modify_write = 1'b1;
1084
                        end
1085
                        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
1086
                                write = 1'b1;
1087
                        end
1088
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
1089
                                read = 1'b1;
1090
                        end
1091
                endcase
1092
        end
1093
endmodule
1094
 
1095
 
1096
 

powered by: WebSVN 2.1.0

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