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

Subversion Repositories cpu8080

[/] [cpu8080/] [trunk/] [project/] [testbench.v] - Blame information for rev 9

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

Line No. Rev Author Line
1 2 samiam9512
`timescale 1ns / 1ps
2
//////////////////////////////////////////////////////////////////////////////////
3
// Company: 
4
// Engineer: 
5
// 
6
// Create Date:    23:25:07 09/20/2006 
7
// Design Name: 
8
// Module Name:    testbench 
9
// Project Name:                            
10
// Target Devices: 
11
// Tool versions: 
12
// Description: 
13
//
14
// Dependencies: 
15
//
16
// Revision: 
17
// Revision 0.01 - File Created
18
// Additional Comments:                                             
19
//
20
//////////////////////////////////////////////////////////////////////////////////
21
 
22
module testbench(addr,     // Address out
23
                 data,     // Data bus
24
                 readmem,  // Memory read
25
                 writemem, // Memory write
26
                 readio,   // Read I/O space
27
                 writeio,  // Write I/O space
28
                 intr,     // Interrupt request 
29
                 inta,     // Interrupt request 
30
                 waitr,    // Wait request
31
                 reset,    // Reset
32
                 clock);   // System clock
33
 
34
   output [15:0] addr;
35
   inout  [7:0] data;
36
   output readmem;
37
   output writemem;
38
   output readio;
39
   output writeio;
40 9 samiam9512
   output intr;
41 2 samiam9512
   output inta;
42
   input  waitr;
43
   input  reset;
44
   input  clock;
45
 
46 9 samiam9512
   // selector block, we only use select 1, 2 and 3
47
   select select1(addr, data, readio, writeio, romsel, ramsel, intsel,
48 2 samiam9512
                  select4, bootstrap, clock, reset);
49
 
50
   cpu8080 cpu(addr, data, readmem, writemem, readio, writeio, intr, inta, waitr,
51
               reset, clock);
52 9 samiam9512
   rom rom(addr[9:0], data, romsel&readmem); // unclocked rom
53
   // neg clocked ram
54
   ram ram(addr[9:0], data, ramsel, readmem, writemem, bootstrap, clock);
55
   // neg clocked interrupt controller
56
   intcontrol intc(addr[2:0], data, writeio, readio, intsel, intr, inta, int0, int1,
57
                   int2, int3, int4, int5, int6, int7, reset, clock);
58 2 samiam9512
 
59 9 samiam9512
   // pull up unused interrupt lines
60
   assign int0 = 1;
61
   assign int1 = 1;
62
   assign int2 = 1;
63
   assign int3 = 1;
64
   assign int4 = 1;
65
   assign int5 = 1;
66
   assign int6 = 1;
67
   assign int7 = 1;
68 2 samiam9512
   // fake input device, always returns $42
69
   assign data = readio ? 8'h42: 8'bz;
70
 
71
endmodule
72
 
73
////////////////////////////////////////////////////////////////////////////////
74
//
75
// Peripheral select unit
76
//
77
// This block implements a general purpose select generator. It has 4 select
78
// units, each capable of matching up to 6 bits of address, or 1kb of address
79
// resolution. The length and base address of each generated select can be
80
// specified, and each select can be mapped either to memory or I/O. In the case
81
// of I/O, the match for the select takes place on the lower 8 bits of the
82
// address, corresponding to the 0-255 addresses in the I/O space.
83
// Note that the selects must still be qualified with readmem, writemem,
84
// readio and writeio signals at the selected peripheral.
85
//
86
// The selector itself has an I/O address of 0, but this can be moved as well.
87
// However, the selector must remain in I/O space.
88
//
89
// A special "bootstrap" mode is implemented. After power on, and until the
90
// selector is configured by the processor, both select1 and select2 will be
91
// active, along with the output signal bootstrap. These should be connected as
92
// follows:
93
//
94
// select1:   Connect to bootstrap ROM
95
// select2:   Connect to RAM
96
// bootstrap: Connect to RAM output buffers to disable them when true
97
//
98
// In bootstrap mode, ROM and RAM selects are on until the bootstrap mode is
99
// turned off by a CPU I/O operation. The bootstrap signal indicates that 
100
// bootstrap mode is active, and should be used to disable the RAM output 
101
// buffers.
102
//
103
// Because the ROM does not perform writes, and the RAM is disabled from reads,
104
// the RAM will overlay the ROM in memory, with the ROM providing read data,
105
// and the RAM accepting write data. This is sometimes called "shadow mode".
106
// What it does is allow the CPU to copy the ROM to RAM by performing a block
107
// read and write to the same address, ie., it picks up the content at each
108
// address, then writes it back, effectively copying the ROM contents to the
109
// RAM. The CPU can then program the selects, exit bootstrap mode, and then
110
// execute entirely from the RAM copy of the bootstrap ROM.
111
//
112
// Bootstrapping this way accomplishes a few ends. First, because memory
113
// is limited on a 64kb machine, it allows RAM to occupy all of memory, without
114
// having to reserve a block of space permanently for the boostrap ROM. Second,
115
// ROM is usually a lot slower than RAM nowdays, so it is common to want to
116
// run from RAM instead of trying to execute directly from ROM.
117
//
118
// The reason that we gate the RAM output buffers with the bootstrap signal,
119
// instead of trying to gate the select signal with readmem, is that the latter
120
// would generate glitches, since the readmem signal is enveloped by the 
121
// address, instead of vice versa. It also gives the RAM logic a chance to cut
122
// down on the delay of readmem to output drive enable.
123
//
124
// The Format of the registers in the select unit are:
125
//
126
//    7 6 5 4 3 2 1 0
127
// 0: C C C C X X X B - Main control register
128
// 1: X X X X X X X X - Unused
129
// 2: M M M M M M I E - Select 1 mask
130
// 3: C C C C C C X X - Select 1 compare
131
// 4: M M M M M M I E - Select 2 mask
132
// 5: C C C C C C X X - Select 2 compare
133
// 6: M M M M M M I E - Select 3 mask
134
// 7: C C C C C C X X - Select 3 compare
135
// 8: M M M M M M I E - Select 4 mask
136 9 samiam9512
// 9: C C C C C C X X - Select 4 compare
137 2 samiam9512
//
138
// The main control bits 7:4 contain the one of 16 base addresses for the
139
// select controller. It occupies 16 locations in the address space, of
140
// which only 9 are actually used. The compare bits reset to 0, so that the
141
// select unit occupies the I/O addresses $00-$0A on power up. The base address
142
// can be changed by writing the main control register, and the new address will
143
// take place on the next access. The select unit can only be addressed in the
144
// I/O space.
145
//
146
// The bootstrap bit is reset to 1, and can be written to 0 to turn off 
147
// bootstrap mode.
148
//
149
 
150
module select(addr, data, readio, writeio, select1, select2, select3, select4,
151
              bootstrap, clock, reset);
152
 
153
   input [15:0] addr;      // CPU address
154
   inout [7:0]  data;      // CPU data bus
155
   input        readio;    // I/O read
156
   input        writeio;   // I/O write
157
   output       select1;   // select 1
158
   output       select2;   // select 1
159
   output       select3;   // select 1
160
   output       select4;   // select 1
161
   output       bootstrap; // bootstrap status
162
   input        clock;     // CPU clock
163
   input        reset;     // reset
164
   reg          bootstrap; // bootstrap mode
165
 
166
   reg [7:4]    seladr; // base I/O address of selector
167
   reg [7:0]    datai; // internal data
168
 
169
   assign selacc = seladr == addr[7:4]; // form selector access
170
   assign accmain = selacc && (addr[3:1] == 3'b000); // select main
171
   assign acca = selacc && (addr[3:1] == 3'b001); // select 1
172
   assign accb = selacc && (addr[3:1] == 3'b010); // select 2
173
   assign accc = selacc && (addr[3:1] == 3'b011); // select 3
174
   assign accd = selacc && (addr[3:1] == 3'b100); // select 4
175
 
176
   // Control access to main select unit address. This has to be clocked to
177
   // activate the address only after the cycle is over.
178
   always @(posedge clock)
179
      if (reset) begin
180
 
181
         seladr <= 4'b0; // clear master select
182
         bootstrap <= 1; // enable bootstrap mode
183
 
184
      end else if (writeio&accmain) begin
185
 
186
         seladr <= data[7:4]; // write master select
187
         bootstrap <= data[0]; // write bootstrap mode bit
188
 
189
      end else if (readio&accmain)
190
         datai <= {seladr, 4'b0}; // read master select
191
 
192
   selectone selecta(addr, data, writeio, readio, acca, select1i, reset);
193
   selectone selectb(addr, data, writeio, readio, accb, select2i, reset);
194
   selectone selectc(addr, data, writeio, readio, accc, select3, reset);
195
   selectone selectd(addr, data, writeio, readio, accd, select4, reset);
196
 
197
   assign data = readio&accmain ? datai: 8'bz; // enable output data
198
 
199
   assign select1 = select1i | bootstrap; // enable select 1 via bootstrap
200
   assign select2 = select2i | bootstrap; // enable select 2 via bootstrap
201
 
202
endmodule
203
 
204
//
205
// Individual select cell.
206
//
207
// This cell contains the mask and compare registers for each address. It
208
// handles the write and read of these registers, and forms a select signal
209
// based on them.
210
//
211
// Each register pair has the appearance:
212
//
213
//    7 6 5 4 3 2 1 0 
214
//   ================
215
// 0: M M M M M M I E - Mask register.
216
// 1: C C C C C C X X - Compare register.
217
//
218
// The mask register selects which bits will be used to form the compare
219
// value. This can be used to select any size from the combinations:
220
//
221
// 1 1 1 1 1 1 - Any 1kb block of memory, or 4 I/O address bits.
222
// 1 1 1 1 1 0 - Any 2kb block of memory, or 8 I/O address bits.
223
// 1 1 1 1 0 0 - Any 4kb block of memory, or 16 I/O address bits.
224
// 1 1 1 0 0 0 - Any 8kb block of memory, or 32 I/O address bits.
225
// 1 1 0 0 0 0 - Any 16kb block of memory, or 64 I/O address bits.
226
// 1 0 0 0 0 0 - Any 32kb block of memory, or 128 I/O address bits.
227
// 0 0 0 0 0 0 - All 64kb of memory, or all 256 I/O addresses
228
//
229
// Each block must be on its size, so for example, a 16kb block can only
230
// be on one of 4 positions in memory. If you use a pattern that isn't
231
// listed above, you are on your own to figure out the consequences.
232
// The selector does not weed out bad combinations, and you can select
233
// multiple blocks at once.
234
//
235
// Each of the mask and compare bytes can be both read and written.
236
//
237
// Note that the lower bits of the compare register aren't used, and always
238
// return zero.
239
//
240
// On reset, the mask and compare registers are both set to zero, which leaves
241
// the select block disabled.
242
//
243
 
244
module selectone(addr, data, write, read, selectin, selectout, reset);
245
 
246
   input [15:0] addr;     // address to match, 6 bits
247
   inout [7:0] data;      // CPU data
248
   input       write;     // CPU write
249
   input       read;      // CPU read
250
   input       selectin;  // select for read/write
251
   output      selectout; // resulting select
252
   input       reset;     // reset
253
 
254
   reg  [7:0] mask;  // mask/control, 7:2 is mask, 1: I/O or /mem, 0: on/off
255
   reg  [7:2] comp;  // Compare value
256
   wire [5:0] iaddr; // multiplexed address
257
   reg  [7:0] datai; // data from output selector
258
 
259
   // select what part of address, upper or lower byte, we compare, based on
260
   // I/O or memory address
261
   assign iaddr = mask[1] ? addr[7:2]: addr[15:10];
262
 
263
   // Form select based on match
264
   assign selectout = ((iaddr & mask[7:2]) == comp) & mask[0];
265
 
266 9 samiam9512
   always @(addr, write, read, reset, selectin, data, comp, mask)
267 2 samiam9512
      if (reset) begin
268
 
269
      comp <= 6'b0; // clear registers
270
      mask <= 8'b0;
271
 
272
   end else if (write&selectin) begin
273
 
274
      if (addr[0]) comp <= data[7:2]; // write comparitor data
275
      else mask <= data; // write mask data
276
 
277
   end else begin
278
 
279
      if (addr[0]) datai <= {comp, 2'b0}; // read comparitor data
280
      else datai <= mask; // read mask data
281
 
282
   end
283
 
284
   assign data = read&selectin ? datai: 8'bz; // enable output data
285
 
286
endmodule
287
 
288 9 samiam9512
////////////////////////////////////////////////////////////////////////////////
289
//
290
// INTERRUPT CONTROLLER
291
//
292
// Implements an 8 input interrupt controller. Each of the 8 interrupt lines has
293
// selectable positive edge, negative edge, positive level, and negative level
294
// triggering. Interrupts can be masked, and can be examined for state even when
295
// they are masked. Each interrupt can be triggered under software control.
296
// The priority for interrupts is fixed, with 0 being the highest, and 7 being
297
// the lowest.
298
//
299
// The controller can be connected to I/O or memory addresses. The control
300
// registers appear as:
301
//
302
//     7 6 5 4 3 2 1 0
303
// 00: M M M M M M M M - Mask register
304
// 01: S S S S S S S S - Status register
305
// 02: A A A A A A A A - Active register
306
// 03: P P P P P P P P - Polarity register
307
// 04: E E E E E E E E - Edge enable register
308
// 05: B B B B B B B B - Vector base address
309
//
310
// The mask register indicates if the interrupt source is to generate an 
311
// interrupt. If the associated bit is 1, the interrupt is enabled, otherwise
312
// disabled.
313
//
314
// The status register indicates the current interrupt line status, for direct
315
// polling purposes.
316
//
317
// The active register is a flip/flop that goes to 1 anytime the trigger 
318
// condition is satisfied. A 1 in this register will cause an interrupt to 
319
// occur. If the mask for the interrupt is not enabled, the active bit will not
320
// be set no matter what the trigger states.
321
//
322
// The polarity register gives the line polarity that will trigger an interrupt.
323
// If the edge trigger bit is set, then the polarity indicates the resulting 
324
// line AFTER the trigger. For example, an edge trigger with a 1 in the polarity
325
// register indicates that the trigger is a positive edge trigger.
326
//
327
// The edge enable register places an edge detector on the line. This will
328
// cause the interrupt to be triggered when an appropriate edge indicated by the
329
// polarity occurs. The edge mode is selected by a 1 bit, and the level mode is
330
// selected by a 0 bit. If the level mode is selected, the interrupt will occur
331
// anytime the interrupt line matches the state of the polarity bit.
332
//
333
// The vector registers each provide the lower 8 bits of a 16 bit vector for 
334
// each interrupt.
335
//
336
// The base register provides the upper 8 bits of a 16 bit vector for each
337
// interrupt.
338
//
339
// An interrupt is generated anytime any bit is true in the active register. 
340
// The interrupt request line is set true, and the controller will hold until
341
// an interrupt acknowledge occurs. When an interrupt acknowledge occurs, the
342
// controller will cycle through a three step sequence, with each sequence
343
// activated by the interrupt acknowledge signal.
344
//
345
// First, a $cd is placed on the data lines, indicating a call instruction.
346
// Second, the number of the interrupt that is highest priority is multipled 
347
// * 4, and this is placed on the data lines.
348
// Third, the vector base address is placed on the data lines.
349
//
350
// The net result is that the CPU is vectored to one of 256 pages in the address
351
// space, with an offset of 4 bytes for each interrupt, as follows:
352
//
353
// 00: Vector 0
354
// 04: Vector 1
355
// 08: Vector 2
356
// 0C: Vector 3
357
// 10: Vector 4
358
// 14: Vector 5
359
// 18: Vector 6
360
// 1C: Vector 7
361
//
362
 
363
module intcontrol(addr, data, write, read, select, intr, inta, int0, int1, int2,
364
                  int3, int4, int5, int6, int7, reset, clock);
365
 
366
   input [2:0] addr;   // control register address
367
   inout [7:0] data;   // CPU data
368
   input       write;  // CPU write
369
   input       read;   // CPU read
370
   input       select; // controller select
371
   output      intr;   // interrupt request
372
   input       inta;   // interrupt acknowledge
373
   input       int0;   // interrupt line 0
374
   input       int1;   // interrupt line 1
375
   input       int2;   // interrupt line 2
376
   input       int3;   // interrupt line 3
377
   input       int4;   // interrupt line 4
378
   input       int5;   // interrupt line 5
379
   input       int6;   // interrupt line 6
380
   input       int7;   // interrupt line 7
381
   input       reset;  // CPU reset
382
   input       clock;  // CPU clock
383
 
384
   reg [7:0] mask;     // interrupt mask register
385
   reg [7:0] active;   // interrupt active register
386
   reg [7:0] polarity; // interrupt polarity register
387
   reg [7:0] edges;    // interrupt edge control
388
   reg [7:0] vbase;    // vector base
389
   reg [7:0] intpe;    // positive edge interrupt detection
390
   reg [7:0] intne;    // negative edge interrupt detection
391
   reg [7:0] datai; // data from output selector
392
   reg [3:0] state; // state machine to run vectors
393
 
394
   wire [7:0] activep;  // interrupt active pending
395
 
396
   // handle register reads and writes
397
 
398
   always @(negedge clock)
399
      if (reset) begin // reset
400
 
401
      mask     <= 8'b0; // clear mask
402
      active   <= 8'b0; // clear active
403
      polarity <= 8'b0; // clear polarity
404
      edges    <= 8'b0; // clear edge
405
      vbase    <= 8'b0; // clear base
406
      state    <= 4'b0; // clear state machine
407
 
408
   end else if (write&select) begin // CPU write
409
 
410
      case (addr)
411
 
412
         0: mask     <= data; // set mask register
413
         2: active   <= data|activep; // set active register
414
         3: polarity <= data; // set polarity register
415
         4: edges    <= data; // set edge register
416
         5: vbase    <= data; // set base register
417
 
418
      endcase
419
 
420
   end else if (read&select) begin // CPU read
421
 
422
      case (addr)
423
 
424
         0: datai <= mask; // get mask register
425
         // get current line statuses
426
         1: datai <= { int7, int6, int5, int4, int3, int2, int1, int0 };
427
         2: datai <= active; // get active register
428
         3: datai <= polarity; // get polarity register
429
         4: datai <= edges; // get edge register
430
         5: datai <= vbase; // get base register
431
 
432
      endcase
433
 
434
   end else if (inta) begin // CPU interrupt acknowledge 
435
 
436
      // run vectoring state machine
437
      case (state)
438
 
439
         // wait for inta, and assert 1st instruction byte
440
 
441
         0: begin
442
 
443
            datai <= 8'hcd; // place call instruction on datalines
444
            state <= 1; // advance to low address
445
 
446
         end
447
 
448
         // assert low byte address
449
         1: begin
450
 
451
            // decode priority
452
            if (active&8'h01)      datai <= 8'h00;
453
            else if (active&8'h02) datai <= 8'h04;
454
            else if (active&8'h04) datai <= 8'h08;
455
            else if (active&8'h08) datai <= 8'h0C;
456
            else if (active&8'h10) datai <= 8'h10;
457
            else if (active&8'h20) datai <= 8'h14;
458
            else if (active&8'h40) datai <= 8'h18;
459
            else if (active&8'h80) datai <= 8'h1C;
460
            state <= 2; // advance to high address
461
 
462
         end
463
 
464
         // assert high address
465
         2: if (inta) begin
466
 
467
            datai <= vbase; // place page to vector
468
            // reset highest priority interrupt
469
            if (active&8'h01)      active[0] <= activep[0];
470
            else if (active&8'h02) active[1] <= activep[1];
471
            else if (active&8'h04) active[2] <= activep[2];
472
            else if (active&8'h08) active[3] <= activep[3];
473
            else if (active&8'h10) active[4] <= activep[4];
474
            else if (active&8'h20) active[5] <= activep[5];
475
            else if (active&8'h40) active[6] <= activep[6];
476
            else if (active&8'h80) active[7] <= activep[7];
477
            state <= 0; // back to start state
478
 
479
         end
480
 
481
      endcase
482
 
483
   end else active <= active|activep; // set active interrupts
484
 
485
   // form active interrupt bits
486
   assign activep = mask & (({ int7, int6, int5, int4, // levels
487
                               int3, int2, int1, int0 }^polarity & ~edges)|
488
                           (intpe&polarity&edges)| // positive edges
489
                           (intne&~polarity&edges)); // negative edges
490
 
491
   // form interrupt edges
492
   always @(posedge int0) intpe[0] <= 1;
493
   always @(posedge int1) intpe[1] <= 1;
494
   always @(posedge int2) intpe[2] <= 1;
495
   always @(posedge int3) intpe[3] <= 1;
496
   always @(posedge int4) intpe[4] <= 1;
497
   always @(posedge int5) intpe[5] <= 1;
498
   always @(posedge int6) intpe[6] <= 1;
499
   always @(posedge int7) intpe[7] <= 1;
500
   always @(negedge int0) intne[0] <= 1;
501
   always @(negedge int1) intne[1] <= 1;
502
   always @(negedge int2) intne[2] <= 1;
503
   always @(negedge int3) intne[3] <= 1;
504
   always @(negedge int4) intne[4] <= 1;
505
   always @(negedge int5) intne[5] <= 1;
506
   always @(negedge int6) intne[6] <= 1;
507
   always @(negedge int7) intne[7] <= 1;
508
 
509
   assign data = read&select|inta ? datai: 8'bz; // enable output data
510
   assign intr = |active; // request interrupt on any active
511
 
512
endmodule
513
 
514
////////////////////////////////////////////////////////////////////////////////
515
//
516
// ROM CELL
517
//
518
// Hold the test instructions. Forms a simple read only cell, with tri-state
519
// enable outputs only.
520
//
521
 
522 2 samiam9512
module rom(addr, data, dataeno);
523
 
524
   input [9:0] addr;
525
   inout [7:0] data;
526
   input dataeno;
527
 
528
   reg [7:0] datao;
529
 
530
   always @(addr) case (addr)
531
 
532 9 samiam9512
      `include "test.lst" // get contents of memory
533 2 samiam9512
 
534
      default datao = 8'h76; // hlt
535
 
536
   endcase
537
 
538
   // Enable drive for data output
539
   assign data = dataeno ? datao: 8'bz;
540
 
541
endmodule
542
 
543 9 samiam9512
////////////////////////////////////////////////////////////////////////////////
544
//
545
// RAM CELL
546
//
547
// A clocked ram cell with individual select, read and write signals. Data is
548
// written on the positive edge when write is true. Data is enabled for output
549
// by the read signal asyncronously.
550
//
551
// A bootstrap mode is implemented that, when true, overrides the read signal
552
// and keeps the output drivers off.
553
//
554
 
555 2 samiam9512
module ram(addr, data, select, read, write, bootstrap, clock);
556
 
557
   input [9:0] addr;
558
   inout [7:0] data;
559
   input select;
560
   input read;
561
   input write;
562
   input clock;
563
   input bootstrap;
564
 
565
   reg [7:0] ramcore [1023:0]; // The ram store
566
   reg [7:0] datao;
567
 
568 9 samiam9512
   always @(negedge clock)
569 2 samiam9512
      if (select) begin
570
 
571
         if (write) ramcore[addr] <= data;
572
         datao <= ramcore[addr];
573
 
574
      end
575
 
576
   // Enable drive for data output
577
   assign data = (select&read&~bootstrap) ? datao: 8'bz;
578
 
579
endmodule

powered by: WebSVN 2.1.0

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