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

Subversion Repositories t6507lp

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

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 157 gabrielosh
 
243
                PLA_IMP : begin
244
                        result = alu_a;
245
                end
246 141 creep
 
247
                // STA - Store Accumulator
248
                // PHA - Push A
249
                // TAX - Transfer Accumulator to X
250
                // TAY - Transfer Accumulator to Y
251
                TAX_IMP, TAY_IMP, PHA_IMP, STA_ZPG, STA_ZPX, STA_ABS, STA_ABX, STA_ABY, STA_IDX, STA_IDY : begin
252
                        result = A;
253
                end
254
 
255
                // STX - Store X Register
256
                // TXA - Transfer X to Accumulator
257
                // TXS - Transfer X to Stack pointer
258
                STX_ZPG, STX_ZPY, STX_ABS, TXA_IMP, TXS_IMP : begin
259
                        result = X;
260
                end
261
 
262
                // STY - Store Y Register
263
                // TYA - Transfer Y to Accumulator
264
                STY_ZPG, STY_ZPX, STY_ABS, TYA_IMP : begin
265
                        result = Y;
266
                end
267
 
268
                // SEC - Set Carry Flag
269
                SEC_IMP: begin
270
                        STATUS[C] = 1'b1;
271
                end
272
 
273
                // SED - Set Decimal Flag
274
                SED_IMP: begin
275
                        STATUS[D] = 1'b1;
276
                end
277
 
278
                // SEI - Set Interrupt Disable
279
                SEI_IMP: begin
280
                        STATUS[I] = 1'b1;
281
                end
282
 
283
                // INC - Increment memory
284
                INC_ZPG, INC_ZPX, INC_ABS, INC_ABX : begin
285
                        result = alu_a + 1;
286
                end
287
 
288
                // INX - Increment X Register
289
                INX_IMP: begin
290
                        result = X + 1;
291
                end
292
 
293
                // INY - Increment Y Register
294
                INY_IMP : begin
295
                        result = Y + 1;
296
                end
297
 
298
                // DEC - Decrement memory
299
                DEC_ZPG, DEC_ZPX, DEC_ABS, DEC_ABX : begin
300
                        result = alu_a - 1;
301
                end
302
 
303
                // DEX - Decrement X register
304
                DEX_IMP: begin
305
                        result = X - 1;
306
                end
307
 
308
                // DEY - Decrement Y Register
309
                DEY_IMP: begin
310
                        result = Y - 1;
311
                end
312
 
313
                // ADC - Add with carry
314
                ADC_IMM, ADC_ZPG, ADC_ZPX, ADC_ABS, ADC_ABX, ADC_ABY, ADC_IDX, ADC_IDY : begin
315
                        if (alu_status[D] == 1) begin
316
                                if (A[3:0] > 9) begin
317 152 gabrielosh
                                        op1 = A + 6; // A = A - 10 and A = A + 16
318 141 creep
                                end
319 152 gabrielosh
                                if (op1[7:4] > 9) begin
320
                                        op1 = op1[7:4] + 6; // A = A - 10 and A = A + 16
321 141 creep
                                end
322
                                if (alu_a[3:0] > 9) begin
323 152 gabrielosh
                                        op2 = alu_a + 6;
324 141 creep
                                end
325 152 gabrielosh
                                if (op2[7:4] > 9) begin
326
                                        op2 = op2[7:4] + 6; // A = A - 10 and A = A + 16
327 141 creep
                                end
328
                        end
329 152 gabrielosh
                        {STATUS[C],result} = op1 + op2 + alu_status[C];
330
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
331 141 creep
                                STATUS[V] = 1;
332
                        else
333
                                STATUS[V] = 0;
334 156 gabrielosh
                        $display("op1 + op2 + C = result + C (V)");
335
                        $display("%d  + %d  + %b = %d + %b (%b)", op1, op2, alu_status[C],result,STATUS[C],STATUS[V]);
336 141 creep
 
337
                        if (alu_status[D] == 1) begin
338
                                if (result[3:0] > 9) begin
339
                                        result = result[3:0] + 6; // A = A - 10 and A = A + 16
340
                                end
341
                                if (result[7:4] > 9) begin
342
                                        result = result[7:4] + 6; // A = A - 10 and A = A + 16
343
                                        STATUS[C] = 1;
344
                                end
345
                        end
346
                end
347
 
348
                // AND - Logical AND
349
                AND_IMM, AND_ZPG, AND_ZPX, AND_ABS, AND_ABX, AND_ABY, AND_IDX, AND_IDY : begin
350
                        result = A & alu_a;
351
                end
352
 
353
                // CMP - Compare
354
                CMP_IMM, CMP_ZPG, CMP_ZPX, CMP_ABS, CMP_ABX, CMP_ABY, CMP_IDX, CMP_IDY : begin
355
                        result = A - alu_a;
356
                        STATUS[C] = (A >= alu_a) ? 1 : 0;
357
                end
358
 
359
                // EOR - Exclusive OR
360
                EOR_IMM, EOR_ZPG, EOR_ZPX, EOR_ABS, EOR_ABX, EOR_ABY, EOR_IDX, EOR_IDY : begin
361 156 gabrielosh
                        result = A ^ alu_a;
362
                        $display("op1 ^ op2 = result");
363
                        $display("%d  ^ %d  = %d", op1, op2, result);
364 141 creep
                end
365
 
366
                // LDA - Load Accumulator
367
                // LDX - Load X Register
368
                // LDY - Load Y Register
369
                // TSX - Transfer Stack Pointer to X
370
                LDA_IMM, LDA_ZPG, LDA_ZPX, LDA_ABS, LDA_ABX, LDA_ABY, LDA_IDX, LDA_IDY,
371
                LDX_IMM, LDX_ZPG, LDX_ZPY, LDX_ABS, LDX_ABY,
372
                LDY_IMM, LDY_ZPG, LDY_ZPX, LDY_ABS, LDY_ABX,
373
                TSX_IMP : begin
374
                        result = alu_a;
375
                end
376
 
377
                // ORA - Logical OR
378
                ORA_IMM, ORA_ZPG, ORA_ZPX, ORA_ABS, ORA_ABX, ORA_ABY, ORA_IDX, ORA_IDY : begin
379
                        result = A | alu_a;
380
                end
381
 
382
                // SBC - Subtract with Carry
383
                SBC_IMM, SBC_ZPG, SBC_ZPX, SBC_ABS, SBC_ABX, SBC_ABY, SBC_IDX, SBC_IDY : begin
384
                        if (alu_status[D] == 1) begin
385
                                if (A[3:0] > 9) begin
386 152 gabrielosh
                                        op1 = A + 6; // A = A - 10 and A = A + 16
387 141 creep
                                end
388 152 gabrielosh
                                if (op1[7:4] > 9) begin
389
                                        op1 = op1[7:4] + 6; // A = A - 10 and A = A + 16
390 141 creep
                                end
391
                                if (alu_a[3:0] > 9) begin
392 152 gabrielosh
                                        op2 = alu_a + 6;
393 141 creep
                                end
394 152 gabrielosh
                                if (op2[7:4] > 9) begin
395
                                        op2 = op2[7:4] + 6; // A = A - 10 and A = A + 16
396 141 creep
                                end
397
                        end
398
 
399 152 gabrielosh
                        {STATUS[C],result} = op1 - op2 - ~alu_status[C];
400
 
401
                        if ((op1[7] == op2[7]) && (op1[7] != result[7]))
402 141 creep
                                STATUS[V] = 1;
403
                        else
404
                                STATUS[V] = 0;
405
                end
406
 
407
                // ASL - Arithmetic Shift Left
408
                ASL_ACC : begin
409 145 gabrielosh
                        //{STATUS[C],result} = A << 1;
410
                        {STATUS[C],result} = {A,1'b0};
411 141 creep
                end
412
                ASL_ZPG, ASL_ZPX, ASL_ABS, ASL_ABX : begin
413 145 gabrielosh
                        //{STATUS[C],result} = alu_a << 1;
414
                        {STATUS[C],result} = {alu_a,1'b0};
415 141 creep
                end
416
 
417
                // LSR - Logical Shift Right
418
                LSR_ACC: begin
419 145 gabrielosh
                        //{result, STATUS[C]} = A >> 1;
420
                        {result,STATUS[C]} = {1'b0,A};
421 141 creep
                end
422
                LSR_ZPG, LSR_ZPX, LSR_ABS, LSR_ABX : begin
423 145 gabrielosh
                        //{result, STATUS[C]} = alu_a >> 1;
424
                        {result,STATUS[C]} = {1'b0,alu_a};
425 141 creep
                end
426
 
427
                // ROL - Rotate Left
428
                ROL_ACC : begin
429 152 gabrielosh
                        {STATUS[C],result} = {A,alu_status[C]};
430 141 creep
                end
431
                ROL_ZPG, ROL_ZPX, ROL_ABS, ROL_ABX : begin
432
                        {STATUS[C],result} = {alu_a,alu_status[C]};
433
                end
434
 
435 152 gabrielosh
                // ROR - Rotate Right
436 141 creep
                ROR_ACC : begin
437
                        {result,STATUS[C]} = {alu_status[C],A};
438
                end
439
                ROR_ZPG, ROR_ZPX, ROR_ABS, ROR_ABX : begin
440
                        {result, STATUS[C]} = {alu_status[C], alu_a};
441
                end
442
 
443
                // CPX - Compare X Register
444
                CPX_IMM, CPX_ZPG, CPX_ABS : begin
445
                        result = X - alu_a;
446
                        STATUS[C] = (X >= alu_a) ? 1 : 0;
447
                end
448
 
449
                // CPY - Compare Y Register
450
                CPY_IMM, CPY_ZPG, CPY_ABS : begin
451
                        result = Y - alu_a;
452
                        STATUS[C] = (Y >= alu_a) ? 1 : 0;
453
                end
454
 
455
                default: begin // NON-DEFAULT OPCODES FALL HERE
456 142 gabrielosh
                end
457 141 creep
        endcase
458 142 gabrielosh
        STATUS[Z] = (result == 0) ? 1 : 0;
459
        STATUS[N] = result[7];
460 141 creep
end
461
 
462
endmodule
463
 

powered by: WebSVN 2.1.0

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