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

Subversion Repositories t6507lp

[/] [t6507lp/] [trunk/] [rtl/] [verilog/] [t6507lp_alu.v] - Blame information for rev 154

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

Line No. Rev Author Line
1 141 creep
////////////////////////////////////////////////////////////////////////////
2 152 gabrielosh
////                                                                    ////
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 ALU                                                           ////
10
////                                                                    ////
11
//// To Do:                                                             ////
12
//// - Search for TODO                                                  ////
13
////                                                                    ////
14
//// Author(s):                                                         ////
15
//// - Gabriel Oshiro Zardo, gabrieloshiro@gmail.com                    ////
16
//// - Samuel Nascimento Pagliarini (creep), snpagliarini@gmail.com     ////
17
////                                                                    ////
18 141 creep
////////////////////////////////////////////////////////////////////////////
19 152 gabrielosh
////                                                                    ////
20
//// Copyright (C) 2001 Authors and OPENCORES.ORG                       ////
21
////                                                                    ////
22
//// This source file may be used and distributed without               ////
23
//// restriction provided that this copyright statement is not          ////
24
//// removed from the file and that any derivative work contains        ////
25
//// the original copyright notice and the associated disclaimer.       ////
26
////                                                                    ////
27
//// This source file is free software; you can redistribute it         ////
28
//// and/or modify it under the terms of the GNU Lesser General         ////
29
//// Public License as published by the Free Software Foundation;       ////
30
//// either version 2.1 of the License, or (at your option) any         ////
31
//// later version.                                                     ////
32
////                                                                    ////
33
//// This source is distributed in the hope that it will be             ////
34
//// useful, but WITHOUT ANY WARRANTY; without even the implied         ////
35
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR            ////
36
//// PURPOSE. See the GNU Lesser General Public License for more        ////
37
//// details.                                                           ////
38
////                                                                    ////
39
//// You should have received a copy of the GNU Lesser General          ////
40
//// Public License along with this source; if not, download it         ////
41
//// from http://www.opencores.org/lgpl.shtml                           ////
42
////                                                                    ////
43 141 creep
////////////////////////////////////////////////////////////////////////////
44
 
45
`include "timescale.v"
46
 
47
// TODO: verify code identation
48
 
49
module t6507lp_alu( clk, reset_n, alu_enable, alu_result, alu_status, alu_opcode, alu_a, alu_x, alu_y );
50
 
51
input wire       clk;
52
input wire       reset_n;
53
input wire       alu_enable;
54
input wire [7:0] alu_opcode;
55
input wire [7:0] alu_a;
56
output reg [7:0] alu_result;
57
output reg [7:0] alu_status;
58
output reg [7:0] alu_x;
59
output reg [7:0] alu_y;
60
 
61
reg [7:0] A;
62
reg [7:0] X;
63
reg [7:0] Y;
64
 
65
reg [7:0] STATUS;
66
reg [7:0] result;
67 152 gabrielosh
reg [7:0] op1;
68
reg [7:0] op2;
69 141 creep
 
70
`include "t6507lp_package.v"
71
 
72
always @ (posedge clk or negedge reset_n)
73
begin
74
        if (reset_n == 0) begin
75
                alu_result <= 0;
76
                alu_status[C] <= 0;
77
                alu_status[N] <= 0;
78
                alu_status[V] <= 0;
79 148 gabrielosh
                alu_status[5] <= 1;
80 141 creep
                alu_status[Z] <= 1;
81
                alu_status[I] <= 0;
82
                alu_status[B] <= 0;
83
                alu_status[D] <= 0;
84
                A <= 0;
85
                X <= 0;
86
                Y <= 0;
87
                alu_x <= 0;
88
                alu_y <= 0;
89
        end
90
        else if ( alu_enable == 1 ) begin
91
                case (alu_opcode)
92
                        ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY,
93
                        AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY,
94
                        ASL_ACC, EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX,
95
                        EOR_IDY, LSR_ACC, ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY,
96
                        ORA_IDX, ORA_IDY, ROL_ACC, ROR_ACC, SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS,
97
                        SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY, LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS,
98
                        LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY, PLA_IMP, TXA_IMP, TYA_IMP :
99
                        begin
100
                                A          <= result;
101
                                alu_result <= result;
102
                                alu_status <= STATUS;
103
                        end
104
                        LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY, TAX_IMP, TSX_IMP, INX_IMP, DEX_IMP :
105
                        begin
106
                                X          <= result;
107
                                alu_x      <= result;
108
                                alu_status <= STATUS;
109
                        end
110
                        TXS_IMP :
111
                        begin
112 148 gabrielosh
                                X          <= result;
113
                                alu_x      <= result;
114 141 creep
                        end
115
                        LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX, TAY_IMP, INY_IMP, DEY_IMP :
116
                        begin
117
                                Y          <= result;
118
                                alu_y      <= result;
119
                                alu_status <= STATUS;
120
                        end
121 148 gabrielosh
                        CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY,
122
                        CPX_IMM, CPX_ZPG, CPX_ABS, CPY_IMM, CPY_ZPG, CPY_ABS, PHP_IMP :
123 141 creep
                        begin
124
                                alu_status <= STATUS;
125
                        end
126
                        SEC_IMP :
127
                        begin
128
                                alu_status[C] <= 1;
129
                        end
130
                        SED_IMP :
131
                        begin
132
                                alu_status[D] <= 1;
133
                        end
134
                        SEI_IMP :
135
                        begin
136
                                alu_status[I] <= 1;
137
                        end
138
                        CLC_IMP :
139
                        begin
140
                                alu_status[C] <= 0;
141
                        end
142
                        CLD_IMP :
143
                        begin
144
                                alu_status[D] <= 0;
145
                        end
146
                        CLI_IMP :
147
                        begin
148
                                alu_status[I] <= 0;
149
                        end
150
                        CLV_IMP :
151
                        begin
152
                                alu_status[V] <= 0;
153
                        end
154
                        BRK_IMP :
155
                        begin
156 154 gabrielosh
                                alu_status[B] <= 1;
157 141 creep
                        end
158
                        PLP_IMP, RTI_IMP :
159
                        begin
160 150 gabrielosh
                                alu_status[C] <= alu_a[C];
161
                                alu_status[Z] <= alu_a[Z];
162
                                alu_status[I] <= alu_a[I];
163
                                alu_status[D] <= alu_a[D];
164
                                alu_status[B] <= alu_a[B];
165
                                alu_status[V] <= alu_a[V];
166
                                alu_status[N] <= alu_a[N];
167 141 creep
                        end
168
                        BIT_ZPG, BIT_ABS :
169
                        begin
170
                                alu_status[Z] <= STATUS[Z];
171
                                alu_status[V] <= alu_a[6];
172
                                alu_status[N] <= alu_a[7];
173
                        end
174 148 gabrielosh
                        INC_ZPG, INC_ZPX, INC_ABS, INC_ABX, DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX,
175
                        ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX, LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX,
176
                        ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX, ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX :
177 141 creep
                        begin
178
                                alu_result <= result;
179
                                alu_status <= STATUS;
180
                        end
181
                        default : begin
182
                                //$display("ERROR");
183
                        end
184
                endcase
185
        end
186
end
187
 
188
always @ (*) begin
189 152 gabrielosh
        op1      = A;
190
        op2      = alu_a;
191 150 gabrielosh
        result    = alu_result;
192
        STATUS[N] = alu_status[N];
193
        STATUS[C] = alu_status[C];
194
        STATUS[V] = alu_status[V];
195
        STATUS[B] = alu_status[B];
196
        STATUS[I] = alu_status[I];
197
        STATUS[D] = alu_status[D];
198
        STATUS[Z] = alu_status[Z];
199
        STATUS[N] = alu_status[N];
200 151 gabrielosh
        STATUS[5] = 1;
201 141 creep
 
202
        case (alu_opcode)
203
                // BIT - Bit Test
204
                BIT_ZPG, BIT_ABS: begin
205
                        result = A & alu_a;
206
                end
207
 
208
                // BRK - Force Interrupt
209
                BRK_IMP: begin
210
                        STATUS[B] = 1'b1;
211
                end
212
 
213
                // CLC - Clear Carry Flag
214
                CLC_IMP: begin
215
                        STATUS[C] = 1'b0;
216
                end
217
 
218
                // CLD - Clear Decimal Flag
219
                CLD_IMP: begin
220
                        STATUS[D] = 1'b0;
221
                end
222
 
223
                // CLI - Clear Interrupt Disable
224
                CLI_IMP: begin
225
                        STATUS[I] = 1'b0;
226
                end
227
 
228
                // CLV - Clear Overflow Flag
229
                CLV_IMP: begin
230
                        STATUS[V] = 1'b0;
231
                end
232
 
233
                // NOP - No Operation
234
                //NOP_IMP: begin
235
                        // Do nothing :-D
236
                //end
237
 
238
                // PLP - Pull Processor Status Register
239
                PLP_IMP, RTI_IMP: begin
240
                        STATUS = alu_a;
241
                end
242
 
243
                // STA - Store Accumulator
244
                // PHA - Push A
245
                // TAX - Transfer Accumulator to X
246
                // TAY - Transfer Accumulator to Y
247
                TAX_IMP, TAY_IMP, PHA_IMP, STA_ZPG, STA_ZPX, STA_ABS, STA_ABX, STA_ABY, STA_IDX, STA_IDY : begin
248
                        result = A;
249
                end
250
 
251
                // STX - Store X Register
252
                // TXA - Transfer X to Accumulator
253
                // TXS - Transfer X to Stack pointer
254
                STX_ZPG, STX_ZPY, STX_ABS, TXA_IMP, TXS_IMP : begin
255
                        result = X;
256
                end
257
 
258
                // STY - Store Y Register
259
                // TYA - Transfer Y to Accumulator
260
                STY_ZPG, STY_ZPX, STY_ABS, TYA_IMP : begin
261
                        result = Y;
262
                end
263
 
264
                // SEC - Set Carry Flag
265
                SEC_IMP: begin
266
                        STATUS[C] = 1'b1;
267
                end
268
 
269
                // SED - Set Decimal Flag
270
                SED_IMP: begin
271
                        STATUS[D] = 1'b1;
272
                end
273
 
274
                // SEI - Set Interrupt Disable
275
                SEI_IMP: begin
276
                        STATUS[I] = 1'b1;
277
                end
278
 
279
                // INC - Increment memory
280
                INC_ZPG, INC_ZPX, INC_ABS, INC_ABX : begin
281
                        result = alu_a + 1;
282
                end
283
 
284
                // INX - Increment X Register
285
                INX_IMP: begin
286
                        result = X + 1;
287
                end
288
 
289
                // INY - Increment Y Register
290
                INY_IMP : begin
291
                        result = Y + 1;
292
                end
293
 
294
                // DEC - Decrement memory
295
                DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX : begin
296
                        result = alu_a - 1;
297
                end
298
 
299
                // DEX - Decrement X register
300
                DEX_IMP: begin
301
                        result = X - 1;
302
                end
303
 
304
                // DEY - Decrement Y Register
305
                DEY_IMP: begin
306
                        result = Y - 1;
307
                end
308
 
309
                // ADC - Add with carry
310
                ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY : begin
311
                        if (alu_status[D] == 1) begin
312
                                if (A[3:0] > 9) begin
313 152 gabrielosh
                                        op1 = A + 6; // A = A - 10 and A = A + 16
314 141 creep
                                end
315 152 gabrielosh
                                if (op1[7:4] > 9) begin
316
                                        op1 = op1[7:4] + 6; // A = A - 10 and A = A + 16
317 141 creep
                                end
318
                                if (alu_a[3:0] > 9) begin
319 152 gabrielosh
                                        op2 = alu_a + 6;
320 141 creep
                                end
321 152 gabrielosh
                                if (op2[7:4] > 9) begin
322
                                        op2 = op2[7:4] + 6; // A = A - 10 and A = A + 16
323 141 creep
                                end
324
                        end
325 152 gabrielosh
                        {STATUS[C],result} = op1 + op2 + alu_status[C];
326
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
327 141 creep
                                STATUS[V] = 1;
328
                        else
329
                                STATUS[V] = 0;
330
 
331
                        if (alu_status[D] == 1) begin
332
                                if (result[3:0] > 9) begin
333
                                        result = result[3:0] + 6; // A = A - 10 and A = A + 16
334
                                end
335
                                if (result[7:4] > 9) begin
336
                                        result = result[7:4] + 6; // A = A - 10 and A = A + 16
337
                                        STATUS[C] = 1;
338
                                end
339
                        end
340
                end
341
 
342
                // AND - Logical AND
343
                AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY : begin
344
                        result = A & alu_a;
345
                end
346
 
347
                // CMP - Compare
348
                CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY : begin
349
                        result = A - alu_a;
350
                        STATUS[C] = (A >= alu_a) ? 1 : 0;
351
                end
352
 
353
                // EOR - Exclusive OR
354
                EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX, EOR_IDY : begin
355
                        result = A ^ alu_a ;
356
                end
357
 
358
                // LDA - Load Accumulator
359
                // LDX - Load X Register
360
                // LDY - Load Y Register
361
                // TSX - Transfer Stack Pointer to X
362
                LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS, LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY,
363
                LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY,
364
                LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX,
365
                TSX_IMP : begin
366
                        result = alu_a;
367
                end
368
 
369
                // ORA - Logical OR
370
                ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY, ORA_IDX, ORA_IDY : begin
371
                        result = A | alu_a;
372
                end
373
 
374
                // SBC - Subtract with Carry
375
                SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS, SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY : begin
376
                        if (alu_status[D] == 1) begin
377
                                if (A[3:0] > 9) begin
378 152 gabrielosh
                                        op1 = A + 6; // A = A - 10 and A = A + 16
379 141 creep
                                end
380 152 gabrielosh
                                if (op1[7:4] > 9) begin
381
                                        op1 = op1[7:4] + 6; // A = A - 10 and A = A + 16
382 141 creep
                                end
383
                                if (alu_a[3:0] > 9) begin
384 152 gabrielosh
                                        op2 = alu_a + 6;
385 141 creep
                                end
386 152 gabrielosh
                                if (op2[7:4] > 9) begin
387
                                        op2 = op2[7:4] + 6; // A = A - 10 and A = A + 16
388 141 creep
                                end
389
                        end
390
 
391 152 gabrielosh
                        {STATUS[C],result} = op1 - op2 - ~alu_status[C];
392
 
393
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
394 141 creep
                                STATUS[V] = 1;
395
                        else
396
                                STATUS[V] = 0;
397
                end
398
 
399
                // ASL - Arithmetic Shift Left
400
                ASL_ACC : begin
401 145 gabrielosh
                        //{STATUS[C],result} = A << 1;
402
                        {STATUS[C],result} = {A,1'b0};
403 141 creep
                end
404
                ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX : begin
405 145 gabrielosh
                        //{STATUS[C],result} = alu_a << 1;
406
                        {STATUS[C],result} = {alu_a,1'b0};
407 141 creep
                end
408
 
409
                // LSR - Logical Shift Right
410
                LSR_ACC: begin
411 145 gabrielosh
                        //{result, STATUS[C]} = A >> 1;
412
                        {result,STATUS[C]} = {1'b0,A};
413 141 creep
                end
414
                LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX : begin
415 145 gabrielosh
                        //{result, STATUS[C]} = alu_a >> 1;
416
                        {result,STATUS[C]} = {1'b0,alu_a};
417 141 creep
                end
418
 
419
                // ROL - Rotate Left
420
                ROL_ACC : begin
421 152 gabrielosh
                        {STATUS[C],result} = {A,alu_status[C]};
422 141 creep
                end
423
                ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX : begin
424
                        {STATUS[C],result} = {alu_a,alu_status[C]};
425
                end
426
 
427 152 gabrielosh
                // ROR - Rotate Right
428 141 creep
                ROR_ACC : begin
429
                        {result,STATUS[C]} = {alu_status[C],A};
430
                end
431
                ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX : begin
432
                        {result, STATUS[C]} = {alu_status[C], alu_a};
433
                end
434
 
435
                // CPX - Compare X Register
436
                CPX_IMM, CPX_ZPG, CPX_ABS : begin
437
                        result = X - alu_a;
438
                        STATUS[C] = (X >= alu_a) ? 1 : 0;
439
                end
440
 
441
                // CPY - Compare Y Register
442
                CPY_IMM, CPY_ZPG, CPY_ABS : begin
443
                        result = Y - alu_a;
444
                        STATUS[C] = (Y >= alu_a) ? 1 : 0;
445
                end
446
 
447
                default: begin // NON-DEFAULT OPCODES FALL HERE
448 142 gabrielosh
                end
449 141 creep
        endcase
450 142 gabrielosh
        STATUS[Z] = (result == 0) ? 1 : 0;
451
        STATUS[N] = result[7];
452 141 creep
end
453
 
454
endmodule
455
 

powered by: WebSVN 2.1.0

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