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

Subversion Repositories t6507lp

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

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

Line No. Rev Author Line
1 61 creep
////////////////////////////////////////////////////////////////////////////
2
////                                                                    ////
3
//// T6507LP IP Core                                                    ////
4
////                                                                    ////
5
//// This file is part of the T6507LP project                           ////
6
//// http://www.opencores.org/cores/t6507lp/                            ////
7
////                                                                    ////
8
//// Description                                                        ////
9
//// 6507 FSM                                                           ////
10
////                                                                    ////
11
//// TODO:                                                              ////
12
//// - Fix absolute indexed mode                                        ////
13
//// - Code the relative mode                                           ////
14
//// - Code the indexed indirect mode                                   ////
15
//// - Code the indirect indexed mode                                   ////
16
//// - Code the absolute indirect mode                                  ////
17
////                                                                    ////
18
//// Author(s):                                                         ////
19
//// - Gabriel Oshiro Zardo, gabrieloshiro@gmail.com                    ////
20
//// - Samuel Nascimento Pagliarini (creep), snpagliarini@gmail.com     ////
21
////                                                                    ////
22
////////////////////////////////////////////////////////////////////////////
23
////                                                                    ////
24
//// Copyright (C) 2001 Authors and OPENCORES.ORG                       ////
25
////                                                                    ////
26
//// This source file may be used and distributed without               ////
27
//// restriction provided that this copyright statement is not          ////
28
//// removed from the file and that any derivative work contains        ////
29
//// the original copyright notice and the associated disclaimer.       ////
30
////                                                                    ////
31
//// This source file is free software; you can redistribute it         ////
32
//// and/or modify it under the terms of the GNU Lesser General         ////
33
//// Public License as published by the Free Software Foundation;       ////
34
//// either version 2.1 of the License, or (at your option) any         ////
35
//// later version.                                                     ////
36
////                                                                    ////
37
//// This source is distributed in the hope that it will be             ////
38
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
39
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
40
//// PURPOSE. See the GNU Lesser General Public License for more        ////
41
//// details.                                                           ////
42
////                                                                    ////
43
//// You should have received a copy of the GNU Lesser General          ////
44
//// Public License along with this source; if not, download it         ////
45
//// from http://www.opencores.org/lgpl.shtml                           ////
46
////                                                                    ////
47
////////////////////////////////////////////////////////////////////////////
48
 
49
`timescale 1ns / 1ps
50
 
51 71 creep
module t6507lp_fsm(clk, reset_n, alu_result, alu_status, data_in, address, control, data_out, alu_opcode, alu_a, alu_enable);
52 82 creep
        parameter DATA_SIZE = 4'h8;
53
        parameter ADDR_SIZE = 4'hd;
54 68 creep
 
55 82 creep
        localparam DATA_SIZE_ = DATA_SIZE - 1'b1;
56
        localparam ADDR_SIZE_ = ADDR_SIZE - 1'b1;
57
 
58 71 creep
        input clk;
59
        input reset_n;
60 82 creep
        input [DATA_SIZE_:0] alu_result;
61
        input [DATA_SIZE_:0] alu_status;
62
        input [DATA_SIZE_:0] data_in;
63
        output reg [ADDR_SIZE_:0] address;
64 61 creep
        output reg control; // one bit is enough? read = 0, write = 1
65 82 creep
        output reg [DATA_SIZE_:0] data_out;
66
        output reg [DATA_SIZE_:0] alu_opcode;
67
        output reg [DATA_SIZE_:0] alu_a;
68 68 creep
        output reg alu_enable;
69 61 creep
 
70 68 creep
 
71 61 creep
        // FSM states
72 68 creep
        localparam RESET = 4'b1111;
73 63 creep
        localparam FETCH_OP = 4'b0000;
74 68 creep
        localparam FETCH_OP_CALC = 4'b0001;
75
        localparam FETCH_LOW = 4'b0010;
76
        localparam FETCH_HIGH = 4'b0011;
77 71 creep
        localparam READ_MEM = 4'b0100;
78
        localparam DUMMY_WRT_CALC = 4'b0101;
79
        localparam WRITE_MEM = 4'b0110;
80
        localparam FETCH_OP_CALC_PARAM = 4'b0111;
81 61 creep
 
82
        // OPCODES TODO: verify how this get synthesised
83
        `include "../T6507LP_Package.v"
84
 
85
        // control signals
86
        localparam MEM_READ = 1'b0;
87
        localparam MEM_WRITE = 1'b1;
88
 
89 82 creep
        reg [ADDR_SIZE_:0] pc;           // program counter
90
        reg [DATA_SIZE_:0] sp;           // stack pointer
91
        reg [DATA_SIZE_:0] ir;           // instruction register
92
        reg [ADDR_SIZE_:0] temp_addr;    // temporary address
93
        reg [DATA_SIZE_:0] temp_data;    // temporary data
94 61 creep
 
95
        reg [3:0] state, next_state; // current and next state registers
96
        // TODO: not sure if this will be 4 bits wide. as of march 9th this was 4bit wide.
97
 
98
        // wiring that simplifies the FSM logic
99
        reg absolute;
100
        reg absolute_indexed;
101
        reg accumulator;
102
        reg immediate;
103
        reg implied;
104
        reg indirect;
105
        reg relative;
106
        reg zero_page;
107
        reg zero_page_indexed;
108
 
109
        // regs that store the type of operation. again, this simplifies the FSM a lot.
110
        reg read;
111
        reg read_modify_write;
112
        reg write;
113
        reg jump;
114
 
115 82 creep
        wire [ADDR_SIZE_:0] next_pc;
116 63 creep
        assign next_pc = pc + 13'b0000000000001;
117 61 creep
 
118 71 creep
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
119
                if (reset_n == 1'b0) begin
120
                        // all registers must assume default values
121 68 creep
                        pc <= 0; // TODO: this is written somewhere. something about a reset vector. must be checked.
122
                        sp <= 0; // TODO: the default is not 0. maybe $0100 or something like that. must be checked.
123 82 creep
                        ir <= 8'h00;
124 71 creep
                        temp_addr <= 0;
125 82 creep
                        temp_data <= 8'h00;
126 68 creep
                        state <= RESET;
127 71 creep
                        // registered outputs also receive default values
128
                        address <= 0;
129 82 creep
                        control <= MEM_READ;
130
                        data_out <= 8'h00;
131 61 creep
                end
132
                else begin
133
                        state <= next_state;
134 71 creep
                        control <= MEM_READ;
135 82 creep
                        data_out <= 8'h00;
136 61 creep
                        case (state)
137 68 creep
                                RESET: begin
138 82 creep
                                        // The processor was reset
139
                                        $write("under reset");
140 68 creep
                                end
141 61 creep
                                FETCH_OP: begin // this state is the simplest one. it is a simple fetch that must be done when the cpu was reset or
142
                                                // the last cycle was a memory write.
143
                                        pc <= next_pc;
144 71 creep
                                        address <= next_pc;
145 70 creep
                                        ir <= data_in;
146 61 creep
                                end
147 71 creep
                                FETCH_OP_CALC, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
148 61 creep
                                        pc <= next_pc;
149 71 creep
                                        address <= next_pc;
150 70 creep
                                        ir <= data_in;
151 61 creep
                                end
152 68 creep
                                FETCH_LOW: begin // in this state the opcode is already known so truly execution begins
153
                                        if (accumulator || implied) begin
154 70 creep
                                                pc <= pc; // is this better?
155 71 creep
                                                address <= pc;
156 61 creep
                                        end
157 70 creep
                                        else if (immediate) begin
158 68 creep
                                                pc <= next_pc;
159 71 creep
                                                address <= next_pc;
160 70 creep
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
161 61 creep
                                        end
162 71 creep
                                        else if (absolute) begin
163
                                                pc <= next_pc;
164
                                                address <= next_pc;
165
                                                temp_addr[7:0] <= data_in;
166
                                        end
167 77 creep
                                        else if (zero_page) begin
168
                                                pc <= next_pc;
169
                                                address <= {{5{1'b0}},data_in};
170 78 creep
                                                temp_addr <= {{5{1'b0}},data_in};
171
 
172 77 creep
                                                if (write) begin
173
                                                        control <= MEM_WRITE;
174 78 creep
                                                        data_out <= alu_result;
175 77 creep
                                                end
176
                                        end
177 61 creep
                                end
178 71 creep
                                FETCH_HIGH: begin
179
                                        if (jump) begin
180
                                                pc <= {data_in[4:0], temp_addr}; // PCL <= first byte, PCH <= second byte
181
                                                address <= {data_in[4:0], temp_addr};
182
                                        end
183
                                        else begin
184
                                                if (write) begin
185
                                                        pc <= next_pc;
186
                                                        temp_addr[12:8] <= data_in[4:0];
187
                                                        address <= {data_in[4:0],temp_addr[7:0]};
188
                                                        control <= MEM_WRITE;
189 77 creep
                                                        data_out <= alu_result;
190 71 creep
                                                end
191
                                                else begin // read_modify_write or just read
192
                                                        pc <= next_pc;
193
                                                        temp_addr[12:8] <= data_in[4:0];
194
                                                        address <= {data_in[4:0],temp_addr[7:0]};
195
                                                end
196
                                        end
197
                                        //else begin
198
                                        //      $write("FETCHHIGH PROBLEM"); 
199
                                        //      $finish(0); 
200
                                        //end
201 61 creep
                                end
202 71 creep
                                READ_MEM: begin
203
                                        if (read_modify_write) begin
204
                                                pc <= pc;
205
                                                address <= temp_addr;
206
                                                control <= MEM_WRITE;
207
                                                temp_data <= data_in;
208
                                                data_out <= data_in; // writeback the same value
209
                                        end
210
                                        else begin
211
                                                pc <= pc;
212
                                                address <= pc;
213
                                                temp_data <= data_in;
214
                                        end
215 70 creep
                                end
216 71 creep
                                DUMMY_WRT_CALC: begin
217
                                        pc <= pc;
218
                                        address <= temp_addr;
219
                                        control <= MEM_WRITE;
220
                                        data_out <= alu_result;
221 70 creep
                                end
222 71 creep
                                WRITE_MEM: begin
223
                                        pc <= pc;
224
                                        address <= pc;
225 70 creep
                                end
226
                                default: begin
227
                                        $write("unknown state");        // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
228
                                        $finish(0);
229
                                end
230
 
231
                        endcase
232
                end
233
        end
234
 
235 71 creep
        always @ (*) begin // this is the next_state logic and the output logic always block
236
                alu_opcode = 8'h00;
237
                alu_a = 8'h00;
238
                alu_enable = 1'b0;
239 70 creep
 
240 71 creep
                next_state = RESET; // this prevents the latch
241 68 creep
 
242 71 creep
                case (state)
243
                        RESET: begin
244
                                next_state = FETCH_OP;
245
                        end
246
                        FETCH_OP: begin
247
                                next_state = FETCH_LOW;
248
                        end
249 82 creep
                        //FETCH_OP_CALC: begin // so far no addressing mode required the use of this state
250
                        //      next_state = FETCH_LOW;
251
                        //      alu_opcode = ir;
252
                        //      alu_enable = 1'b1;
253
                        //end
254 71 creep
                        FETCH_OP_CALC_PARAM: begin
255
                                next_state = FETCH_LOW;
256
                                alu_opcode = ir;
257
                                alu_enable = 1'b1;
258
                                alu_a = temp_data;
259
                        end
260
                        FETCH_LOW: begin
261
                                if (accumulator  || implied) begin
262
                                        alu_opcode = ir;
263
                                        alu_enable = 1'b1;
264
                                        next_state = FETCH_OP;
265
                                end
266
                                else if (immediate) begin
267
                                        next_state = FETCH_OP_CALC_PARAM;
268
                                end
269 77 creep
                                else if (zero_page) begin
270
                                        if (read || read_modify_write) begin
271
                                                next_state = READ_MEM;
272
                                        end
273
                                        else if (write) begin
274
                                                next_state = WRITE_MEM;
275
                                        end
276
                                        else begin
277
                                                $write("unknown behavior");
278
                                                $finish(0);
279
                                        end
280
                                end
281 71 creep
                                else begin // at least the absolute address mode falls here
282
                                        next_state = FETCH_HIGH;
283
                                end
284 78 creep
 
285
                                if (write) begin
286
                                        alu_opcode = ir;
287
                                        alu_enable = 1'b1;
288 82 creep
                                        alu_a = 8'h00;
289 78 creep
                                end
290
 
291
 
292 71 creep
                        end
293
                        FETCH_HIGH: begin
294
                                if (jump) begin
295 68 creep
                                        next_state = FETCH_OP;
296 61 creep
                                end
297 71 creep
                                else if (read || read_modify_write) begin
298
                                        next_state = READ_MEM;
299 61 creep
                                end
300 71 creep
                                else if (write) begin
301
                                        next_state = WRITE_MEM;
302 68 creep
                                end
303 71 creep
                                else begin
304
                                        $write("unknown behavior");
305
                                        $finish(0);
306 61 creep
                                end
307 71 creep
                        end
308
                        READ_MEM: begin
309
                                if (read) begin
310
                                        next_state = FETCH_OP_CALC_PARAM;
311 61 creep
                                end
312 71 creep
                                else if (read_modify_write) begin
313
                                        next_state = DUMMY_WRT_CALC;
314
                                end
315
                        end
316
                        DUMMY_WRT_CALC: begin
317
                                alu_opcode = ir;
318
                                alu_enable = 1'b1;
319
                                alu_a = data_in;
320
                                next_state = WRITE_MEM;
321
                        end
322
                        WRITE_MEM: begin
323
                                next_state = FETCH_OP;
324
                        end
325
                        default: begin
326
                                next_state = RESET;
327
                        end
328
                endcase
329 61 creep
        end
330
 
331 77 creep
        // this always block is responsible for updating the address mode and the type of operation being done
332 68 creep
        always @ (*) begin // 
333 61 creep
                absolute = 1'b0;
334
                absolute_indexed = 1'b0;
335
                accumulator = 1'b0;
336
                immediate = 1'b0;
337
                implied = 1'b0;
338
                indirect = 1'b0;
339
                relative = 1'b0;
340
                zero_page = 1'b0;
341
                zero_page_indexed = 1'b0;
342
 
343
                read = 1'b0;
344
                read_modify_write = 1'b0;
345
                write = 1'b0;
346
                jump = 1'b0;
347
 
348 70 creep
                case (ir)
349
                        BRK_IMP, CLC_IMP, CLD_IMP, CLI_IMP, CLV_IMP, DEX_IMP, DEY_IMP, INX_IMP, INY_IMP, NOP_IMP, PHA_IMP, PHP_IMP, PLA_IMP,
350
                        PLP_IMP, RTI_IMP, RTS_IMP, SEC_IMP, SED_IMP, SEI_IMP, TAX_IMP, TAY_IMP, TSX_IMP, TXA_IMP, TXS_IMP, TYA_IMP: begin
351
                                implied = 1'b1;
352
                        end
353
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
354
                                accumulator = 1'b1;
355
                        end
356
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
357
                                immediate = 1'b1;
358
                        end
359
                        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,
360
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
361
                                zero_page = 1'b1;
362
                        end
363
                        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,
364
                        SBC_ZPX, STA_ZPX, LDX_ZPY, STX_ZPY, STY_ZPX: begin
365
                                zero_page_indexed = 1'b1;
366
                        end
367
                        BCC_REL, BCS_REL, BEQ_REL, BMI_REL, BNE_REL, BPL_REL, BVC_REL, BVS_REL: begin
368
                                relative = 1'b1;
369
                        end
370
                        ADC_ABS, AND_ABS, ASL_ABS, BIT_ABS, CMP_ABS, CPX_ABS, CPY_ABS, DEC_ABS, EOR_ABS, INC_ABS, JMP_ABS, JSR_ABS, LDA_ABS,
371
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
372
                                absolute = 1'b1;
373
                        end
374
                        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,
375
                        SBC_ABX, STA_ABX, ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
376
                                absolute_indexed = 1'b1;
377
                        end
378
                        ADC_IDX, AND_IDX, CMP_IDX, EOR_IDX, LDA_IDX, ORA_IDX, SBC_IDX, STA_IDX, ADC_IDY, AND_IDY, CMP_IDY, EOR_IDY, LDA_IDY,
379
                        ORA_IDY, SBC_IDY, STA_IDY: begin // all these opcodes are 8'hX1; TODO: optimize this
380
                                indirect = 1'b1;
381
                        end
382 71 creep
                        default: begin
383 77 creep
                                $write("\nunknown OPCODE!!!!! 0x%h\n", ir);
384 71 creep
                                $finish();
385
                        end
386 70 creep
                endcase
387 71 creep
 
388
                case (ir)
389
                        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,
390
                        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,
391
                        DEC_ABX: begin
392
                                read_modify_write = 1'b1;
393
                        end
394
                        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
395
                                write = 1'b1;
396
                        end
397
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
398
                                read = 1'b1;
399
                        end
400
                endcase
401 61 creep
 
402 71 creep
                if (ir == JMP_ABS || ir == JMP_IND) begin // the opcodes are 8'h4C and 8'h6C
403 70 creep
                        jump = 1'b1;
404
                end
405 63 creep
        end // no way
406 61 creep
endmodule
407
 
408
 
409
 

powered by: WebSVN 2.1.0

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