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

Subversion Repositories t6507lp

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

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

powered by: WebSVN 2.1.0

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