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

Subversion Repositories light8080

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

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

Line No. Rev Author Line
1 65 motilito
//---------------------------------------------------------------------------------------
2
// light8080 : Intel 8080 binary compatible core
3
//---------------------------------------------------------------------------------------
4
// v1.3    (12 FEB 2012) Fix: General solution to AND, OR, XOR clearing CY,ACY.
5
// v1.2    (08 jul 2010) Fix: XOR operations were not clearing CY,ACY.
6
// v1.1    (20 sep 2008) Microcode bug in INR fixed.
7
// v1.0    (05 nov 2007) First release. Jose A. Ruiz.
8
//
9
// This file and all the light8080 project files are freeware (See COPYING.TXT)
10
//---------------------------------------------------------------------------------------
11
//
12
// vma :      enable a memory or io r/w access.
13
// io :       access in progress is io (and not memory) 
14
// rd :       read memory or io 
15
// wr :       write memory or io
16
// data_out : data output
17
// addr_out : memory and io address
18
// data_in :  data input
19
// halt :     halt status (1 when in halt state)
20
// inte :     interrupt status (1 when enabled)
21
// intr :     interrupt request
22
// inta :     interrupt acknowledge
23
// reset :    synchronous reset
24
// clk :      clock
25
//
26
// (see timing diagrams at bottom of file)
27
//---------------------------------------------------------------------------------------
28
//
29
// Timing diagram 1: RD and WR cycles
30
//
31
//            1     2     3     4     5     6     7     8     
32
//             __    __    __    __    __    __    __    __   
33
// clk      __/  \__/  \__/  \__/  \__/  \__/  \__/  \__/  \__
34
//
35
//          ==|=====|=====|=====|=====|=====|=====|=====|=====|
36
//
37
// addr_o   xxxxxxxxxxxxxx< ADR >xxxxxxxxxxx< ADR >xxxxxxxxxxx
38
//
39
// data_i   xxxxxxxxxxxxxxxxxxxx< Din >xxxxxxxxxxxxxxxxxxxxxxx
40
//
41
// data_o   xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx< Dout>xxxxxxxxxxx
42
//                         _____             _____
43
// vma_o    ______________/     \___________/     \___________
44
//                         _____
45
// rd_o     ______________/     \_____________________________
46
//                                           _____
47
// wr_o     ________________________________/     \___________
48
//
49
// (functional diagram, actual time delays not shown)
50
////////////////////////////////////////////////////////////////////////////////
51
// This diagram shows a read cycle and a write cycle back to back.
52
// In clock edges (4) and (7), the address is loaded into the external 
53
// synchronous RAM address register. 
54
// In clock edge (5), read data is loaded into the CPU.
55
// In clock edge (7), write data is loaded into the external synchronous RAM.
56
// In actual operation, the CPU does about 1 rd/wr cycle for each 5 clock 
57
// cycles, which is a waste of RAM bandwidth.
58
//
59
//---------------------------------------------------------------------------------------
60
 
61
module light8080
62
(
63
        clk, reset,
64
        addr_out, vma,
65
        io, rd,
66
        wr, fetch,
67
        data_in, data_out,
68
        inta, inte,
69
        halt, intr
70
);
71
 
72
//---------------------------------------------------------------------------------------
73
// 
74
// All memory and io accesses are synchronous (rising clock edge). Signal vma 
75
// works as the master memory and io synchronous enable. More specifically:
76
//
77
//    * All memory/io control signals (io,rd,wr) are valid only when vma is 
78
//      high. They never activate when vma is inactive. 
79
//    * Signals data_out and address are only valid when vma=1'b1. The high 
80
//      address byte is 0x00 for all io accesses.
81
//    * Signal data_in should be valid by the end of the cycle after vma=1'b1, 
82
//      data is clocked in by the rising clock edge.
83
//
84
// All signals are assumed to be synchronous to the master clock. Prevention of
85
// metastability, if necessary, is up to you.
86
// 
87
// Signal reset needs to be active for just 1 clock cycle (it is sampled on a 
88
// positive clock edge and is subject to setup and hold times).
89
// Once reset is deasserted, the first fetch at address 0x0000 will happen 4
90
// cycles later.
91
//
92
// Signal intr is sampled on all positive clock edges. If asserted when inte is
93
// high, interrupts will be disabled, inta will be asserted high and a fetch 
94
// cycle will occur immediately after the current instruction ends execution,
95
// except if intr was asserted at the last cycle of an instruction. In that case
96
// it will be honored after the next instruction ends.
97
// The fetched instruction will be executed normally, except that PC will not 
98
// be valid in any subsequent fetch cycles of the same instruction, 
99
// and will not be incremented (In practice, the same as the original 8080).
100
// inta will remain high for the duration of the fetched instruction, including
101
// fetch and execution time (in the original 8080 it was high only for the 
102
// opcode fetch cycle). 
103
// PC will not be autoincremented while inta is high, but it can be explicitly 
104
// modified (e.g. RST, CALL, etc.). Again, the same as the original.
105
// Interrupts will be disabled upon assertion of inta, and remain disabled 
106
// until explicitly enabled by the program (as in the original).
107
// If intr is asserted when inte is low, the interrupt will not be attended but
108
// it will be registered in an int_pending flag, so it will be honored when 
109
// interrupts are enabled.
110
// 
111
//
112
// The above means that any instruction can be supplied in an inta cycle, 
113
// either single byte or multibyte. See the design notes.
114
//---------------------------------------------------------------------------------------
115
 
116
//---------------------------------------------------------------------------------------
117
// module interfaces 
118
input                   clk;
119
input                   reset;
120
 
121
output  [15:0]   addr_out;
122
output                  vma;
123
output                  io;
124
output                  rd;
125
output                  wr;
126
output                  fetch;
127
 
128
input   [7:0]    data_in;
129
output  [7:0]    data_out;
130
 
131
output                  inta;
132
output                  inte;
133
output                  halt;
134
input                   intr;
135
 
136
//---------------------------------------------------------------------------------------
137
// internal signals 
138
// addr_low: low byte of address
139
reg [7:0] addr_low;
140
// IR: instruction register. some bits left unused.  
141
reg [7:0] IR;
142
// s_field: IR field, sss source reg code
143
wire [2:0] s_field;
144
// d_field: IR field, ddd destination reg code
145
wire [2:0] d_field;
146
// p_field: IR field, pp 16-bit reg pair code
147
wire [1:0] p_field;
148
// rbh: 1 when p_field=11, used in reg bank addressing for 'special' regs
149
wire rbh;                               // 1 when P=11 (special case)  
150
// alu_op: uinst field, ALU operation code 
151
wire [3:0] alu_op;
152
// uc_addr: microcode (ucode) address 
153
reg [7:0] uc_addr;
154
// next_uc_addr: computed next microcode address (uaddr++/jump/ret/fetch)
155
reg [8:0] next_uc_addr;
156
// uc_jmp_addr: uinst field, absolute ucode jump address
157
wire [7:0] uc_jmp_addr;
158
// uc_ret_address: ucode return address saved in previous jump
159
reg [7:0] uc_ret_addr;
160
// addr_plus_1: uaddr + 1
161
wire [7:0] addr_plus_1;
162
// do_reset: reset, delayed 1 cycle // used to reset the microcode sequencer
163
reg do_reset;
164
 
165
// uc_flags1: uinst field, encoded flag of group 1 (see ucode file)
166
wire [2:0] uc_flags1;
167
// uc_flags2: uinst field, encoded flag of group 2 (see ucode file)
168
wire [2:0] uc_flags2;
169
// uc_addr_sel: selection of next uc_addr, composition of 4 flags
170
wire [3:0] uc_addr_sel;
171
// NOTE: see microcode file for information on flags
172
wire uc_jsr;            // uinst field, decoded 'jsr' flag
173
wire uc_tjsr;           // uinst field, decoded 'tjsr' flag
174
wire uc_decode;         // uinst field, decoded 'decode' flag
175
wire uc_end;            // uinst field, decoded 'end' flag
176
reg condition_reg;      // registered tjst condition
177
// condition: tjsr condition (computed ccc condition from '80 instructions)
178
reg condition;
179
// condition_sel: IR field, ccc condition code
180
wire uc_do_jmp;         // uinst jump (jsr/tjsr) flag, pipelined
181
wire uc_do_ret;         // ret flag, pipelined
182
wire uc_halt_flag;      // uinst field, decoded 'halt' flag
183
wire uc_halt;           // halt command
184
reg halt_reg;           // halt status reg, output as 'halt' signal
185
wire uc_ei;                     // uinst field, decoded 'ei' flag
186
wire uc_di;                     // uinst field, decoded 'di' flag
187
reg inte_reg;           // inte status reg, output as 'inte' signal
188
reg int_pending;        // intr requested, inta not active yet
189
reg inta_reg;           // inta status reg, output as 'inta'
190
wire clr_t1;            // uinst field, explicitly erase T1
191
wire do_clr_t1;         // clr_t1 pipelined
192
wire clr_t2;            // uinst field, explicitly erase T2
193
wire do_clr_t2;         // clr_t2 pipelined
194
wire [31:0] ucode;       // microcode word
195
reg [24:0] ucode_field2; // pipelined microcode
196
// used to delay interrup enable for one entire instruction after EI
197
reg delayed_ei;
198
 
199
wire load_al;           // uinst field, load AL reg from rbank
200
wire load_addr;         // uinst field, enable external addr reg load
201
wire load_t1;           // uinst field, load reg T1 
202
wire load_t2;           // uinst field, load reg T2
203
wire mux_in;            // uinst field, T1/T2 input data selection
204
wire load_do;           // uinst field, pipelined, load DO reg
205
// rb_addr_sel: uinst field, rbank address selection: (sss,ddd,pp,ra_field)
206
wire [1:0] rb_addr_sel;
207
// ra_field: uinst field, explicit reg bank address
208
wire [3:0] ra_field;
209
wire [7:0] rbank_data;   // rbank output
210
reg [7:0] alu_output;    // ALU output
211
// data_output: datapath output: ALU output vs. F reg 
212
wire [7:0] data_output;
213
reg [7:0] T1;            // T1 reg (ALU operand)
214
reg [7:0] T2;            // T2 reg (ALU operand)
215
// alu_input: data loaded into T1, T2: rbank data vs. DI
216
wire [7:0] alu_input;
217
wire we_rb;                                                     // uinst field, commands a write to the rbank
218
wire inhibit_pc_increment;                      // avoid PC changes (during INTA)
219
reg [3:0] rbank_rd_addr;                         // rbank rd addr
220
wire [3:0] rbank_wr_addr;                        // rbank wr addr
221
reg [7:0] DO;                                            // data output reg
222
 
223
// Register bank : BC, DE, HL, AF, [PC, XY, ZW, SP]
224
// This will be implemented as asynchronous LUT RAM in those devices where this
225
// feature is available (Xilinx) and as multiplexed registers where it isn't
226
// (Altera).
227
reg [7:0] rbank[0:15];
228
 
229
reg [7:0] flag_reg;              // F register
230
// flag_pattern: uinst field, F update pattern: which flags are updated
231
wire [1:0] flag_pattern;
232
wire flag_s;                    // new computed S flag  
233
wire flag_z;                    // new computed Z flag
234
wire flag_p;                    // new computed P flag
235
wire flag_cy;                   // new computed C flag
236
wire flag_cy_1;                 // C flag computed from arith/logic operation
237
wire flag_cy_2;                 // C flag computed from CPC circuit
238
wire do_cy_op;                  // ALU explicit CY operation (CPC, etc.)
239
wire do_cy_op_d;                // do_cy_op, pipelined
240
wire do_cpc;                    // ALU operation is CPC
241
wire do_cpc_d;                  // do_cpc, pipelined
242
wire do_daa;                    // ALU operation is DAA
243
wire clear_cy;                  // Instruction unconditionally clears CY
244
wire clear_ac;                  // Instruction unconditionally clears AC
245
wire set_ac;                    // Instruction unconditionally sets AC
246
wire flag_ac;                   // new computed half carry flag
247
// flag_aux_cy: new computed half carry flag (used in 16-bit ops)
248
wire flag_aux_cy;
249
wire load_psw;                  // load F register
250
 
251
// aux carry computation and control signals
252
wire use_aux;                   // decoded from flags in 1st phase
253
wire use_aux_cy;                // 2nd phase signal
254
reg reg_aux_cy;
255
wire aux_cy_in;
256
wire set_aux_cy;
257
wire set_aux;
258
 
259
// ALU control signals, together they select ALU operation
260
wire [1:0] alu_fn;
261
wire use_logic;                 // logic/arith mux control 
262
wire [1:0] mux_fn;
263
wire use_psw;                   // ALU/F mux control
264
 
265
// ALU arithmetic operands and result
266
wire [8:0] arith_op1;
267
wire [8:0] arith_op2;
268
wire [8:0] arith_op2_sgn;
269
wire [8:0] arith_res;
270
wire [7:0] arith_res8;
271
 
272
// ALU DAA intermediate signals (DAA has fully dedicated logic)
273
wire [8:0] daa_res;
274
reg [8:0] daa_res9;
275
wire daa_test1;
276
wire daa_test1a;
277
wire daa_test2;
278
wire [7:0] arith_daa_res;
279
wire cy_daa;
280
 
281
// ALU CY flag intermediate signals
282
wire cy_in_sgn;
283
wire cy_in;
284
wire cy_in_gated;
285
wire cy_adder;
286
wire cy_arith;
287
wire cy_shifter;
288
 
289
// ALU intermediate results
290
reg [7:0] logic_res;
291
wire [7:0] shift_res;
292
wire [7:0] alu_mux1;
293
 
294
//---------------------------------------------------------------------------------------
295
// module implementation 
296
// IR register, load when uc_decode flag activates 
297
always @ (posedge clk)
298
begin
299
        if (uc_decode)
300
                IR <= data_in;
301
end
302
 
303
assign s_field = IR[2:0]; // IR field extraction : sss reg code
304
assign d_field = IR[5:3]; // ddd reg code
305
assign p_field = IR[5:4]; // pp 16-bit reg pair code   
306
 
307
//---------------------------------------------------------------------------------------
308
// Microcode sequencer
309
// do_reset is reset delayed 1 cycle
310
always @ (posedge clk)
311
        do_reset <= reset;
312
 
313
assign uc_flags1 = ucode[31:29];
314
assign uc_flags2 = ucode[28:26];
315
 
316
// microcode address control flags are gated by do_reset (reset has priority)
317
assign uc_do_ret = ((uc_flags2 == 3'b011) && !do_reset) ? 1'b1 : 1'b0;
318
assign uc_jsr    = ((uc_flags2 == 3'b010) && !do_reset) ? 1'b1 : 1'b0;
319
assign uc_tjsr   = ((uc_flags2 == 3'b100) && !do_reset) ? 1'b1 : 1'b0;
320
assign uc_decode = ((uc_flags1 == 3'b001) && !do_reset) ? 1'b1 : 1'b0;
321
assign uc_end    = (((uc_flags2 == 3'b001) || (uc_tjsr && !condition_reg)) && !do_reset) ? 1'b1 : 1'b0;
322
 
323
// other microinstruction flags are decoded
324
assign uc_halt_flag = (uc_flags1 == 3'b111) ? 1'b1 : 1'b0;
325
assign uc_halt = (uc_halt_flag && !inta_reg) ? 1'b1 : 1'b0;
326
assign uc_ei   = (uc_flags1 == 3'b011) ? 1'b1 : 1'b0;
327
assign uc_di   = ((uc_flags1 == 3'b010) || inta_reg) ? 1'b1 : 1'b0;
328
// clr_t1/2 clears T1/T2 when explicitly commanded; T2 and T1 clear implicitly 
329
// at the end of each instruction (by uc_decode)
330
assign clr_t2  = (uc_flags2 == 3'b001) ? 1'b1 : 1'b0;
331
assign clr_t1  = (uc_flags1 == 3'b110) ? 1'b1 : 1'b0;
332
assign use_aux = (uc_flags1 == 3'b101) ? 1'b1 : 1'b0;
333
assign set_aux = (uc_flags2 == 3'b111) ? 1'b1 : 1'b0;
334
 
335
assign load_al = ucode[24];
336
assign load_addr = ucode[25];
337
 
338
assign do_cy_op_d = (ucode[5:2] == 4'b1011) ? 1'b1 : 1'b0; // decode CY ALU op
339
assign do_cpc_d = ucode[0];      // decode CPC ALU op
340
 
341
// uinst jump command, either unconditional or on a given condition
342
assign uc_do_jmp = uc_jsr | (uc_tjsr & condition_reg);
343
 
344
assign vma = load_addr;  // addr is valid, either for memory or io
345
 
346
// assume the only uinst that does memory access in the range 0..f is 'fetch'
347
assign fetch = ((uc_addr[7:4] == 4'b0) && load_addr) ? 1'b1 : 1'b0;
348
 
349
// external bus interface control signals
350
assign io = (uc_flags1 == 3'b100) ? 1'b1 : 1'b0; // IO access (vs. memory)
351
assign rd = (uc_flags2 == 3'b101) ? 1'b1 : 1'b0; // RD access
352
assign wr = (uc_flags2 == 3'b110) ? 1'b1 : 1'b0; // WR access  
353
 
354
assign uc_jmp_addr = {ucode[11:10], ucode[5:0]};
355
assign uc_addr_sel = {uc_do_ret, uc_do_jmp, uc_decode, uc_end};
356
assign addr_plus_1 = uc_addr + 8'd1;
357
 
358
// TODO simplify this!!
359
 
360
// NOTE: when end==1'b1 we jump either to the FETCH ucode or to the HALT ucode
361
// depending on the value of the halt signal.
362
// We use the unregistered uc_halt instead of halt_reg because otherwise #end
363
// should be on the cycle following #halt, wasting a cycle.
364
// This means that the flag #halt has to be used with #end or will be ignored. 
365
// Note how we used DI (containing instruction opcode) as a microcode address
366
always @ (*)
367
begin
368
        case (uc_addr_sel)
369
                4'b1000:        next_uc_addr <= {1'b0, uc_ret_addr};    // ret                        
370
                4'b0100:        next_uc_addr <= {1'b0, uc_jmp_addr};    // jsr/tjsr                   
371
                4'b0000:        next_uc_addr <= {1'b0, addr_plus_1};    // uaddr++                    
372
                4'b0001:        next_uc_addr <= {6'b0, uc_halt, 2'b11}; // end: go to fetch/halt uaddr
373
                default:        next_uc_addr <= {1'b1, data_in};                // decode fetched address 
374
        endcase
375
end
376
 
377
// read microcode rom is implemented here in a different module 
378
micro_rom rom
379
(
380
        .clock(clk),
381
        .uc_addr(next_uc_addr),
382
        .uc_dout(ucode)
383
);
384
 
385
// microcode address register
386
always @ (posedge clk)
387
begin
388
        if (reset)
389
                uc_addr <= 8'h0;
390
        else
391
                uc_addr <= next_uc_addr[7:0];
392
end
393
 
394
// ucode address 1-level 'return stack'
395
always @ (posedge clk)
396
begin
397
        if (reset)
398
                uc_ret_addr <= 8'h0;
399
        else if (uc_do_jmp)
400
                uc_ret_addr <= addr_plus_1;
401
end
402
 
403
assign alu_op = ucode[3:0];
404
 
405
// pipeline uinst field2 for 1-cycle delayed execution.
406
// note the same rbank addr field is used in cycles 1 and 2; this enforces
407
// some constraints on uinst programming but simplifies the system.
408
always @ (posedge clk)
409
begin
410
        ucode_field2 <= {do_cy_op_d, do_cpc_d, clr_t2, clr_t1,
411
                                          set_aux, use_aux, rbank_rd_addr, ucode[14:4], alu_op};
412
end
413
 
414
//---------------------------------------------------------------------------------------
415
// HALT logic
416
always @ (posedge clk)
417
begin
418
        if (reset || int_pending)       //inta_reg
419
                halt_reg <= 1'b0;
420
        else if (uc_halt)
421
                halt_reg <= 1'b1;
422
end
423
 
424
assign halt = halt_reg;
425
 
426
//---------------------------------------------------------------------------------------
427
// INTE logic // inte_reg = 1'b1 means interrupts ENABLED
428
always @ (posedge clk)
429
begin
430
        if (reset)
431
        begin
432
                inte_reg <= 1'b0;
433
                delayed_ei <= 1'b0;
434
        end
435
        else
436
        begin
437
                if ((uc_di || uc_ei) && uc_end)
438
                begin
439
                        //inte_reg <= uc_ei;
440
                        delayed_ei <= uc_ei; // FIXME DI must not be delayed
441
                end
442
 
443
                // at the last cycle of every instruction...
444
                if (uc_end)
445
                begin
446
                        // ...disable interrupts if the instruction is DI...
447
                        if (uc_di)
448
                                inte_reg <= 1'b0;
449
                        else
450
                        // ...of enable interrupts after the instruction following EI
451
                                inte_reg <= delayed_ei;
452
                end
453
        end
454
end
455
 
456
assign inte = inte_reg;
457
 
458
// interrupts are ignored when inte=1'b0 but they are registered and will be
459
// honored when interrupts are enabled
460
always @ (posedge clk)
461
begin
462
        if (reset)
463
                int_pending <= 1'b0;
464
        else
465
        begin
466
                // intr will raise int_pending only if inta has not been asserted. 
467
                // Otherwise, if intr overlapped inta, we'd enter a microcode endless 
468
                // loop, executing the interrupt vector again and again.
469
                if (intr && inte_reg && !int_pending && !inta_reg)
470
                        int_pending <= 1'b1;
471
                else if (inte_reg && uc_end)
472
                        // int_pending is cleared when we're about to service the interrupt, 
473
                        // that is when interrupts are enabled and the current instruction ends.
474
                        int_pending <= 1'b0;
475
        end
476
end
477
 
478
//---------------------------------------------------------------------------------------
479
// INTA logic
480
// INTA goes high from END to END, that is for the entire time the instruction
481
// takes to fetch and execute; in the original 8080 it was asserted only for 
482
// the M1 cycle.
483
// All instructions can be used in an inta cycle, including XTHL which was
484
// forbidden in the original 8080. 
485
// It's up to you figuring out which cycle is which in multibyte instructions.
486
always @ (posedge clk)
487
begin
488
        if (reset)
489
                inta_reg <= 1'b0;
490
        else if (int_pending && uc_end)
491
                // enter INTA state
492
                inta_reg <= 1'b1;
493
        else if (uc_end && !uc_halt_flag)
494
                // exit INTA state
495
                // NOTE: don't reset inta when exiting halt state (uc_halt_flag=1'b1).
496
                // If we omit this condition, when intr happens on halt state, inta
497
                // will only last for 1 cycle, because in halt state uc_end is 
498
                // always asserted.
499
                inta_reg <= 1'b0;
500
end
501
 
502
assign inta = inta_reg;
503
 
504
//---------------------------------------------------------------------------------------
505
// Datapath
506
 
507
// extract pipelined microcode fields
508
assign ra_field = ucode[18:15];
509
assign load_t1 = ucode[23];
510
assign load_t2 = ucode[22];
511
assign mux_in = ucode[21];
512
assign rb_addr_sel = ucode[20:19];
513
assign load_do = ucode_field2[7];
514
assign set_aux_cy = ucode_field2[20];
515
assign do_clr_t1 = ucode_field2[21];
516
assign do_clr_t2 = ucode_field2[22];
517
 
518
// T1 register 
519
always @ (posedge clk)
520
begin
521
        if (reset || uc_decode || do_clr_t1)
522
                T1 <= 8'h0;
523
        else if (load_t1)
524
                T1 <= alu_input;
525
end
526
 
527
// T2 register
528
always @ (posedge clk)
529
begin
530
        if (reset || uc_decode || do_clr_t2)
531
                T2 <= 8'h0;
532
        else if (load_t2)
533
                T2 <= alu_input;
534
end
535
 
536
// T1/T2 input data mux
537
assign alu_input = mux_in ? rbank_data : data_in;
538
 
539
// register bank address mux logic
540
assign rbh = (p_field == 2'b11) ? 1'b1 : 1'b0;
541
 
542
always @ (*)
543
begin
544
        case (rb_addr_sel)
545
                2'b00:  rbank_rd_addr <= ra_field;
546
                2'b01:  rbank_rd_addr <= {1'b0, s_field};
547
                2'b10:  rbank_rd_addr <= {1'b0, d_field};
548
                2'b11:  rbank_rd_addr <= {rbh, p_field, ra_field[0]};
549
        endcase
550
end
551
 
552
// RBank writes are inhibited in INTA state, but only for PC increments.
553
assign inhibit_pc_increment = (inta_reg && use_aux_cy && (rbank_wr_addr[3:1] == 3'b100)) ? 1'b1 : 1'b0;
554
assign we_rb = ucode_field2[6] & ~inhibit_pc_increment;
555
 
556
// Register bank logic 
557
// NOTE: read is asynchronous, while write is synchronous; but note also
558
// that write phase for a given uinst happens the cycle after the read phase.
559
// This way we give the ALU time to do its job.
560
assign rbank_wr_addr = ucode_field2[18:15];
561
always @ (posedge clk)
562
begin
563
        if (we_rb)
564
                rbank[rbank_wr_addr] <= alu_output;
565
end
566
assign rbank_data = rbank[rbank_rd_addr];
567
 
568
// should we read F register or ALU output?
569
assign use_psw = (ucode_field2[5:4] == 2'b11) ? 1'b1 : 1'b0;
570
assign data_output = use_psw ? flag_reg : alu_output;
571
 
572
always @ (posedge clk)
573
begin
574
        if (load_do)
575
                DO <= data_output;
576
end
577
 
578
//---------------------------------------------------------------------------------------
579
// ALU 
580
assign alu_fn = ucode_field2[1:0];
581
assign use_logic = ucode_field2[2];
582
assign mux_fn = ucode_field2[4:3];
583
//#### make sure this is "00" in the microcode when no F updates should happen!
584
assign flag_pattern =  ucode_field2[9:8];
585
assign use_aux_cy = ucode_field2[19];
586
assign do_cpc = ucode_field2[23];
587
assign do_cy_op = ucode_field2[24];
588
assign do_daa = (ucode_field2[5:2] == 4'b1010) ? 1'b1 : 1'b0;
589
 
590
// ucode_field2(14) will be set for those instructions that modify CY and AC
591
// without following the standard rules -- AND, OR and XOR instructions.
592
// Some instructions will unconditionally clear CY (AND, OR, XOR)
593
assign clear_cy = ucode_field2[14];
594
// Some instructions will unconditionally clear AC (OR, XOR)...
595
assign clear_ac = (ucode_field2[14] && (ucode_field2[5:0] != 6'b000100)) ? 1'b1 : 1'b0;
596
// ...and some others unconditionally SET AC (AND)
597
assign set_ac = (ucode_field2[14] && (ucode_field2[5:0] == 6'b000100)) ? 1'b1 : 1'b0;
598
 
599
assign aux_cy_in = (!set_aux_cy) ? reg_aux_cy : 1'b1;
600
 
601
// carry input selection: normal or aux (for 16 bit increments)?
602
assign cy_in = (!use_aux_cy) ? flag_reg[0] : aux_cy_in;
603
 
604
// carry is not used (0) in add/sub operations
605
assign cy_in_gated = cy_in & alu_fn[0];
606
 
607
//---------------------------------------------------------------------------------------
608
// Adder/substractor
609
 
610
// zero extend adder operands to 9 bits to ease CY output synthesis
611
// use zero extension because we're only interested in cy from 7 to 8
612
assign arith_op1 = {1'b0, T2};
613
assign arith_op2 = {1'b0, T1};
614
 
615
// The adder/substractor is done in 2 stages to help XSL synth it properly
616
// Other codings result in 1 adder + a substractor + 1 mux
617
 
618
// do 2nd op 2's complement if substracting...
619
assign arith_op2_sgn = (!alu_fn[1]) ? arith_op2 : ~arith_op2;
620
// ...and complement cy input too
621
assign cy_in_sgn = (!alu_fn[1]) ? cy_in_gated : ~cy_in_gated;
622
 
623
// once 2nd operand has been negated (or not) add operands normally
624
assign arith_res = arith_op1 + arith_op2_sgn + cy_in_sgn;
625
 
626
// take only 8 bits; 9th bit of adder is cy output
627
assign arith_res8 = arith_res[7:0];
628
assign cy_adder = arith_res[8];
629
 
630
//---------------------------------------------------------------------------------------
631
// DAA dedicated logic
632
// Note a DAA takes 2 cycles to complete! 
633
 
634
//daa_test1a=1'b1 when daa_res9(7:4) > 0x06
635
assign daa_test1a = arith_op2[3] & (arith_op2[2] | arith_op2[1] | arith_op2[0]);
636
assign daa_test1 = (flag_reg[4] || daa_test1a) ? 1'b1 : 1'b0;
637
 
638
always @ (posedge clk)
639
begin
640
        if (reset)
641
                daa_res9 <= 9'b0;
642
        else if (daa_test1)
643
                daa_res9 <= arith_op2 + 9'd6;
644
        else
645
                daa_res9 <= arith_op2;
646
end
647
 
648
assign daa_test2 = (flag_reg[0] || daa_test1a) ? 1'b1 : 1'b0;
649
 
650
assign daa_res = daa_test2 ? ({1'b0, daa_res9[7:0]} + 9'h60) : daa_res9;
651
 
652
assign cy_daa = daa_res[8];
653
 
654
// DAA vs. adder mux
655
assign arith_daa_res = do_daa ? daa_res[7:0] : arith_res8;
656
 
657
// DAA vs. adder CY mux
658
assign cy_arith = do_daa ? cy_daa : cy_adder;
659
 
660
//---------------------------------------------------------------------------------------
661
// Logic operations block
662
always @ (*)
663
begin
664
        case (alu_fn)
665
                2'b00:  logic_res <= T1 & T2;
666
                2'b01:  logic_res <= T1 ^ T2;
667
                2'b10:  logic_res <= T1 | T2;
668
                2'b11:  logic_res <= ~T1;
669
        endcase
670
end
671
 
672
//---------------------------------------------------------------------------------------
673
// Shifter
674
assign shift_res[6:1] = (!alu_fn[0]) ? T1[5:0] : T1[7:2];
675
 
676
assign shift_res[0] = (alu_fn == 2'b00) ? T1[7] :        // rot left 
677
                      (alu_fn == 2'b10) ? cy_in :       // rot left through carry
678
                                          T1[1];        // rot right
679
assign shift_res[7] = (alu_fn == 2'b01) ? T1[0] :        // rot right
680
                      (alu_fn == 2'b11) ? cy_in :       // rot right through carry
681
                                          T1[6];        // rot left
682
 
683
assign cy_shifter = (!alu_fn[0]) ? T1[7] :               // left
684
                                   T1[0];                // right
685
 
686
assign alu_mux1 = use_logic ? logic_res : shift_res;
687
 
688
always @ (*)
689
begin
690
        case (mux_fn)
691
                2'b00:  alu_output <= alu_mux1;
692
                2'b01:  alu_output <= arith_daa_res;
693
                2'b10:  alu_output <= ~alu_mux1;
694
                2'b11:  alu_output <= {2'b0, d_field, 3'b0};    // RST  
695
        endcase
696
end
697
 
698
//---------------------------------------------------------------------------------------
699
// flag computation 
700
assign flag_s = alu_output[7];
701
assign flag_p = ~(^alu_output);
702
assign flag_z = (alu_output == 8'h0) ? 1'b1 : 1'b0;
703
 
704
// AC is either the CY from bit 4 OR 0 if the instruction clears it implicitly
705
assign flag_ac = set_ac    ? 1'b1 :
706
                 clear_ac  ? 1'b0 :
707
                 (arith_op1[4] ^ arith_op2_sgn[4] ^ alu_output[4]);
708
 
709
// CY comes from the adder or the shifter, or is 0 if the instruction 
710
// implicitly clears it.
711
assign flag_cy_1 = clear_cy   ? 1'b0      :
712
                   use_logic  ? cy_arith  :
713
                   cy_shifter;
714
 
715
assign flag_cy_2 = (!do_cpc) ? ~flag_reg[0] : 1'b1; // cmc, stc
716
assign flag_cy = (!do_cy_op) ? flag_cy_1 : flag_cy_2;
717
 
718
assign flag_aux_cy = cy_adder;
719
 
720
// auxiliary carry reg
721
always @ (posedge clk)
722
begin
723
        if (reset || uc_decode)
724
                reg_aux_cy <= 1'b1; // inits to 0 every instruction
725
        else
726
                reg_aux_cy <= flag_aux_cy;
727
end
728
 
729
// load PSW from ALU (i.e. POP AF) or from flag signals
730
assign load_psw = (we_rb && (rbank_wr_addr == 4'b0110)) ? 1'b1 : 1'b0;
731
 
732
// The F register has been split in two separate groups that always update
733
// together (C and all others).
734
 
735
// F register, flags S,Z,AC,P and C 
736
always @ (posedge clk)
737
begin
738
        if (reset)
739
        begin
740
                flag_reg[7] <= 1'b0;
741
                flag_reg[6] <= 1'b0;
742
                flag_reg[5] <= 1'b0; // constant flag
743
                flag_reg[4] <= 1'b0;
744
                flag_reg[3] <= 1'b0; // constant flag
745
                flag_reg[2] <= 1'b0;
746
                flag_reg[1] <= 1'b1; // constant flag
747
                flag_reg[0] <= 1'b0;
748
        end
749
        else
750
        begin
751
                if (flag_pattern[1])
752
                begin
753
                        if (load_psw)
754
                        begin
755
                                flag_reg[7] <= alu_output[7];
756
                                flag_reg[6] <= alu_output[6];
757
                                flag_reg[4] <= alu_output[4];
758
                                flag_reg[2] <= alu_output[2];
759
                        end
760
                        else
761
                        begin
762
                                flag_reg[7] <= flag_s;
763
                                flag_reg[6] <= flag_z;
764
                                flag_reg[4] <= flag_ac;
765
                                flag_reg[2] <= flag_p;
766
                        end
767
                end
768
 
769
                // C flag               
770
                if (flag_pattern[0])
771
                begin
772
                        if (load_psw)
773
                                flag_reg[0] <= alu_output[0];
774
                        else
775
                                flag_reg[0] <= flag_cy;
776
                end
777
 
778
                // constant flags 
779
                flag_reg[5] <= 1'b0; // constant flag
780
                flag_reg[3] <= 1'b0; // constant flag
781
                flag_reg[1] <= 1'b1; // constant flag
782
        end
783
end
784
 
785
//---------------------------------------------------------------------------------------
786
// Condition computation
787
always @ (*)
788
begin
789
        case (d_field[2:0])
790
                3'b000: condition <= ~flag_reg[6]; // NZ 
791
                3'b001: condition <=  flag_reg[6]; // Z 
792
                3'b010: condition <= ~flag_reg[0]; // NC
793
                3'b011: condition <=  flag_reg[0]; // C 
794
                3'b100: condition <= ~flag_reg[2]; // PO
795
                3'b101: condition <=  flag_reg[2]; // PE
796
                3'b110: condition <= ~flag_reg[7]; // P 
797
                3'b111: condition <=  flag_reg[7]; // M 
798
        endcase
799
end
800
 
801
// condition is registered to shorten the delay path; the extra 1-cycle
802
// delay is not relevant because conditions are tested in the next instruction
803
// at the earliest, and there's at least the fetch uinsts intervening.                
804
always @ (posedge clk)
805
begin
806
        if (reset)
807
                condition_reg <= 1'b0;
808
        else
809
                condition_reg <= condition;
810
end
811
 
812
// low byte address register
813
always @ (posedge clk)
814
begin
815
        if (reset)
816
                addr_low <= 8'h0;
817
        else if (load_al)
818
                addr_low <= rbank_data;
819
end
820
 
821
// note external address registers (high byte) are loaded directly from rbank
822
assign addr_out = {rbank_data, addr_low};
823
 
824
assign data_out = DO;
825
 
826
endmodule
827
//---------------------------------------------------------------------------------------
828
//---------------------------------------------------------------------------------------

powered by: WebSVN 2.1.0

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