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

Subversion Repositories t6507lp

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

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 68 creep
        parameter DATA_SIZE = 4'd8;
53
        parameter ADDR_SIZE = 4'd13;
54
 
55 71 creep
        input clk;
56
        input reset_n;
57 68 creep
        input [DATA_SIZE-1:0] alu_result;
58
        input [DATA_SIZE-1:0] alu_status;
59
        input [DATA_SIZE-1:0] data_in;
60
        output reg [ADDR_SIZE-1:0] address;
61 61 creep
        output reg control; // one bit is enough? read = 0, write = 1
62 68 creep
        output reg [DATA_SIZE-1:0] data_out;
63
        output reg [DATA_SIZE-1:0] alu_opcode;
64
        output reg [DATA_SIZE-1:0] alu_a;
65
        output reg alu_enable;
66 61 creep
 
67 68 creep
 
68 61 creep
        // FSM states
69 68 creep
        localparam RESET = 4'b1111;
70 63 creep
        localparam FETCH_OP = 4'b0000;
71 68 creep
        localparam FETCH_OP_CALC = 4'b0001;
72
        localparam FETCH_LOW = 4'b0010;
73
        localparam FETCH_HIGH = 4'b0011;
74 71 creep
        localparam READ_MEM = 4'b0100;
75
        localparam DUMMY_WRT_CALC = 4'b0101;
76
        localparam WRITE_MEM = 4'b0110;
77
        localparam FETCH_OP_CALC_PARAM = 4'b0111;
78 61 creep
 
79
        // OPCODES TODO: verify how this get synthesised
80
        `include "../T6507LP_Package.v"
81
 
82
        // control signals
83
        localparam MEM_READ = 1'b0;
84
        localparam MEM_WRITE = 1'b1;
85
 
86 68 creep
        reg [ADDR_SIZE-1:0] pc;          // program counter
87
        reg [DATA_SIZE-1:0] sp;          // stack pointer
88
        reg [DATA_SIZE-1:0] ir;          // instruction register
89 71 creep
        reg [ADDR_SIZE:0] temp_addr;     // temporary address
90 68 creep
        reg [DATA_SIZE-1:0] temp_data;   // temporary data
91 61 creep
 
92
        reg [3:0] state, next_state; // current and next state registers
93
        // TODO: not sure if this will be 4 bits wide. as of march 9th this was 4bit wide.
94
 
95
        // wiring that simplifies the FSM logic
96
        reg absolute;
97
        reg absolute_indexed;
98
        reg accumulator;
99
        reg immediate;
100
        reg implied;
101
        reg indirect;
102
        reg relative;
103
        reg zero_page;
104
        reg zero_page_indexed;
105
 
106
        // regs that store the type of operation. again, this simplifies the FSM a lot.
107
        reg read;
108
        reg read_modify_write;
109
        reg write;
110
        reg jump;
111
 
112 68 creep
        wire [ADDR_SIZE-1:0] next_pc;
113 63 creep
        assign next_pc = pc + 13'b0000000000001;
114 61 creep
 
115 71 creep
        always @ (posedge clk or negedge reset_n) begin // sequencial always block
116
                if (reset_n == 1'b0) begin
117
                        // all registers must assume default values
118 68 creep
                        pc <= 0; // TODO: this is written somewhere. something about a reset vector. must be checked.
119
                        sp <= 0; // TODO: the default is not 0. maybe $0100 or something like that. must be checked.
120
                        ir <= 0;
121 71 creep
                        temp_addr <= 0;
122 68 creep
                        temp_data <= 0;
123
                        state <= RESET;
124 71 creep
                        // registered outputs also receive default values
125
                        address <= 0;
126
                        control <= 0; // check if these 2 shouldnt be on the other always block along with the address
127
                        data_out <= 0;
128 61 creep
                end
129
                else begin
130
                        state <= next_state;
131 71 creep
                        control <= MEM_READ;
132 61 creep
 
133
                        case (state)
134 68 creep
                                RESET: begin
135
                                        // The processor was reset 
136
                                end
137 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
138
                                                // the last cycle was a memory write.
139
                                        pc <= next_pc;
140 71 creep
                                        address <= next_pc;
141 70 creep
                                        ir <= data_in;
142 61 creep
                                end
143 71 creep
                                FETCH_OP_CALC, FETCH_OP_CALC_PARAM: begin // this is the pipeline happening!
144 61 creep
                                        pc <= next_pc;
145 71 creep
                                        address <= next_pc;
146 70 creep
                                        ir <= data_in;
147 61 creep
                                end
148 68 creep
                                FETCH_LOW: begin // in this state the opcode is already known so truly execution begins
149
                                        if (accumulator || implied) begin
150 70 creep
                                                pc <= pc; // is this better?
151 71 creep
                                                address <= pc;
152 61 creep
                                        end
153 70 creep
                                        else if (immediate) begin
154 68 creep
                                                pc <= next_pc;
155 71 creep
                                                address <= next_pc;
156 70 creep
                                                temp_data <= data_in; // the follow-up byte is saved in temp_data 
157 61 creep
                                        end
158 71 creep
                                        else if (absolute) begin
159
                                                pc <= next_pc;
160
                                                address <= next_pc;
161
                                                temp_addr[7:0] <= data_in;
162
                                        end
163 61 creep
                                end
164 71 creep
                                FETCH_HIGH: begin
165
                                        if (jump) begin
166
                                                pc <= {data_in[4:0], temp_addr}; // PCL <= first byte, PCH <= second byte
167
                                                address <= {data_in[4:0], temp_addr};
168
                                        end
169
                                        else begin
170
                                                if (write) begin
171
                                                        pc <= next_pc;
172
                                                        temp_addr[12:8] <= data_in[4:0];
173
                                                        address <= {data_in[4:0],temp_addr[7:0]};
174
                                                        control <= MEM_WRITE;
175
                                                end
176
                                                else begin // read_modify_write or just read
177
                                                        pc <= next_pc;
178
                                                        temp_addr[12:8] <= data_in[4:0];
179
                                                        address <= {data_in[4:0],temp_addr[7:0]};
180
                                                end
181
                                        end
182
                                        //else begin
183
                                        //      $write("FETCHHIGH PROBLEM"); 
184
                                        //      $finish(0); 
185
                                        //end
186 61 creep
                                end
187 71 creep
                                READ_MEM: begin
188
                                        if (read_modify_write) begin
189
                                                pc <= pc;
190
                                                address <= temp_addr;
191
                                                control <= MEM_WRITE;
192
                                                temp_data <= data_in;
193
                                                data_out <= data_in; // writeback the same value
194
                                        end
195
                                        else begin
196
                                                pc <= pc;
197
                                                address <= pc;
198
                                                temp_data <= data_in;
199
                                        end
200 70 creep
                                end
201 71 creep
                                DUMMY_WRT_CALC: begin
202
                                        pc <= pc;
203
                                        address <= temp_addr;
204
                                        control <= MEM_WRITE;
205
                                        data_out <= alu_result;
206 70 creep
                                end
207 71 creep
                                WRITE_MEM: begin
208
                                        pc <= pc;
209
                                        address <= pc;
210
                                        data_out = 8'hzz;
211 70 creep
                                end
212
                                default: begin
213
                                        $write("unknown state");        // TODO: check if synth really ignores this 2 lines. Otherwise wrap it with a `ifdef 
214
                                        $finish(0);
215
                                end
216
 
217
                        endcase
218
                end
219
        end
220
 
221 71 creep
        always @ (*) begin // this is the next_state logic and the output logic always block
222 70 creep
                //control = MEM_READ; 
223
                //data_out = 8'h00;
224 71 creep
 
225
                alu_opcode = 8'h00;
226
                alu_a = 8'h00;
227
                alu_enable = 1'b0;
228
                //address = pc;
229 70 creep
 
230 71 creep
                next_state = RESET; // this prevents the latch
231 68 creep
 
232 71 creep
                case (state)
233
                        RESET: begin
234
                                next_state = FETCH_OP;
235
                        end
236
                        FETCH_OP: begin
237
                                next_state = FETCH_LOW;
238
                                //address = pc;
239
                        end
240
                        FETCH_OP_CALC: begin
241
                                next_state = FETCH_LOW;
242
                                alu_opcode = ir;
243
                                alu_enable = 1'b1;
244
                                //address = next_pc;
245
 
246
                        end
247
                        FETCH_OP_CALC_PARAM: begin
248
                                next_state = FETCH_LOW;
249
                                alu_opcode = ir;
250
                                alu_enable = 1'b1;
251
                                alu_a = temp_data;
252
                                //address = next_pc;
253
                        end
254
                        FETCH_LOW: begin
255
                                if (accumulator  || implied) begin
256
                                        alu_opcode = ir;
257
                                        alu_enable = 1'b1;
258
                                        next_state = FETCH_OP;
259
                                end
260
                                else if (immediate) begin
261
                                        next_state = FETCH_OP_CALC_PARAM;
262
                                end
263
                                else begin // at least the absolute address mode falls here
264
                                        next_state = FETCH_HIGH;
265
                                end
266
                                //address = next_pc;
267
 
268
                        end
269
                        FETCH_HIGH: begin
270
                                if (jump) begin
271 68 creep
                                        next_state = FETCH_OP;
272 61 creep
                                end
273 71 creep
                                else if (read || read_modify_write) begin
274
                                        next_state = READ_MEM;
275 61 creep
                                end
276 71 creep
                                else if (write) begin
277
                                        next_state = WRITE_MEM;
278 68 creep
                                end
279 71 creep
                                else begin
280
                                        $write("unknown behavior");
281
                                        $finish(0);
282 61 creep
                                end
283 71 creep
                        end
284
                        READ_MEM: begin
285
                                if (read) begin
286
                                        next_state = FETCH_OP_CALC_PARAM;
287 61 creep
                                end
288 71 creep
                                else if (read_modify_write) begin
289
                                        next_state = DUMMY_WRT_CALC;
290
                                end
291
                        end
292
                        DUMMY_WRT_CALC: begin
293
                                alu_opcode = ir;
294
                                alu_enable = 1'b1;
295
                                alu_a = data_in;
296
                                next_state = WRITE_MEM;
297
                        end
298
                        WRITE_MEM: begin
299
                                next_state = FETCH_OP;
300
                        end
301
                        default: begin
302
                                next_state = RESET;
303
                        end
304
                endcase
305 61 creep
        end
306
 
307
        // this always block is responsible for updating the address mode
308 68 creep
        always @ (*) begin // 
309 61 creep
                absolute = 1'b0;
310
                absolute_indexed = 1'b0;
311
                accumulator = 1'b0;
312
                immediate = 1'b0;
313
                implied = 1'b0;
314
                indirect = 1'b0;
315
                relative = 1'b0;
316
                zero_page = 1'b0;
317
                zero_page_indexed = 1'b0;
318
 
319
                read = 1'b0;
320
                read_modify_write = 1'b0;
321
                write = 1'b0;
322
                jump = 1'b0;
323
 
324 70 creep
                case (ir)
325
                        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,
326
                        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
327
                                implied = 1'b1;
328
                        end
329
                        ASL_ACC, LSR_ACC, ROL_ACC, ROR_ACC: begin
330
                                accumulator = 1'b1;
331
                        end
332
                        ADC_IMM, AND_IMM, CMP_IMM, CPX_IMM, CPY_IMM, EOR_IMM, LDA_IMM, LDX_IMM, LDY_IMM, ORA_IMM, SBC_IMM: begin
333
                                immediate = 1'b1;
334
                        end
335
                        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,
336
                        LSR_ZPG, ORA_ZPG, ROL_ZPG, ROR_ZPG, SBC_ZPG, STA_ZPG, STX_ZPG, STY_ZPG: begin
337
                                zero_page = 1'b1;
338
                        end
339
                        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,
340
                        SBC_ZPX, STA_ZPX, LDX_ZPY, STX_ZPY, STY_ZPX: begin
341
                                zero_page_indexed = 1'b1;
342
                        end
343
                        BCC_REL, BCS_REL, BEQ_REL, BMI_REL, BNE_REL, BPL_REL, BVC_REL, BVS_REL: begin
344
                                relative = 1'b1;
345
                        end
346
                        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,
347
                        LDX_ABS, LDY_ABS, LSR_ABS, ORA_ABS, ROL_ABS, ROR_ABS, SBC_ABS, STA_ABS, STX_ABS, STY_ABS: begin
348
                                absolute = 1'b1;
349
                        end
350
                        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,
351
                        SBC_ABX, STA_ABX, ADC_ABY, AND_ABY, CMP_ABY, EOR_ABY, LDA_ABY, LDX_ABY, ORA_ABY, SBC_ABY, STA_ABY: begin
352
                                absolute_indexed = 1'b1;
353
                        end
354
                        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,
355
                        ORA_IDY, SBC_IDY, STA_IDY: begin // all these opcodes are 8'hX1; TODO: optimize this
356
                                indirect = 1'b1;
357
                        end
358 71 creep
                        default: begin
359
                                $write("unknown OPCODE!");
360
                                $finish();
361
                        end
362 70 creep
                endcase
363 71 creep
 
364
                case (ir)
365
                        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,
366
                        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,
367
                        DEC_ABX: begin
368
                                read_modify_write = 1'b1;
369
                        end
370
                        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
371
                                write = 1'b1;
372
                        end
373
                        default: begin // this should work fine since the previous case statement will detect the unknown/undocumented/unsupported opcodes
374
                                read = 1'b1;
375
                        end
376
                endcase
377 61 creep
 
378 71 creep
                if (ir == JMP_ABS || ir == JMP_IND) begin // the opcodes are 8'h4C and 8'h6C
379 70 creep
                        jump = 1'b1;
380
                end
381 63 creep
        end // no way
382 61 creep
endmodule
383
 
384
 
385
 

powered by: WebSVN 2.1.0

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