OpenCores
URL https://opencores.org/ocsvn/a-z80/a-z80/trunk

Subversion Repositories a-z80

[/] [a-z80/] [trunk/] [cpu/] [toplevel/] [genfuse.py] - Blame information for rev 4

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

Line No. Rev Author Line
1 3 gdevic
#!/usr/bin/env python
2
#
3
# This script generates a test include file from a set of "Fuse" test vectors.
4
#
5
# Three common testing configurations are:
6
#
7
# 1. You want to test a specific instruction only, say 02 LD (BC),A (see Fuse tests.in)
8
#    start_test = "02"
9
#    run_tests = 1
10
#    regress = 0
11
#
12
# 2. You want to run a smaller subset of 'regression' tests:
13
#    start_test = "00"
14
#    run_tests = 1
15
#    regress = 1
16
#
17
# 3. You want to run a full Fuse test suite (all instructions!):
18
#    start_test = "00"
19
#    run_tests = -1
20
#    regress = 0
21
#
22
#-------------------------------------------------------------------------------
23
#  Copyright (C) 2014  Goran Devic
24
#
25
#  This program is free software; you can redistribute it and/or modify it
26
#  under the terms of the GNU General Public License as published by the Free
27
#  Software Foundation; either version 2 of the License, or (at your option)
28
#  any later version.
29
#
30
#  This program is distributed in the hope that it will be useful, but WITHOUT
31
#  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
32
#  FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
33
#  more details.
34
#-------------------------------------------------------------------------------
35
import os
36
 
37
# Start with this test name (this is a string; see tests files)
38
start_test = "00"
39
 
40
# Number of tests to run; use -1 to run all tests
41
run_tests = 1
42
 
43
# Set this to 1 to use regression test files instead of 'tests.*'
44
# It will run all regression tests (start_test, run_tests are ignored)
45
regress = 1
46
 
47
#------------------------------------------------------------------------------
48
# Determine which test files to use
49
tests_in = 'fuse/tests.in'
50
tests_expected = 'fuse/tests.expected'
51
 
52
# Regression testing executes all regression tests
53
if regress:
54
    tests_in = 'fuse/regress.in'
55
    tests_expected = 'fuse/regress.expected'
56
    start_test = "00"
57
    run_tests = -1
58
 
59
with open(tests_in) as f1:
60
    t1 = f1.read().splitlines()
61
# Remove all tests until the one we need to start with. Tests are separated by empty lines.
62
while t1[0].split(" ")[0]!=start_test:
63
    while len(t1.pop(0))>0:
64
        pass
65
t1 = filter(None, t1)   # Filter out empty lines
66
 
67
with open(tests_expected) as f2:
68
    t2 = f2.read().splitlines()
69
while t2[0].split(" ")[0]!=start_test:
70
    while len(t2.pop(0))>0:
71
        pass
72
 
73
# Count total clocks required to run all selected tests
74
total_clks = 0
75
 
76
def RegWrite(reg, hex):
77
    global total_clks
78
    ftest.write("   // Preset " + reg + "\n")
79
    ftest.write("   force dut.reg_file_.b2v_latch_" + reg + "_lo.we=1;\n")
80
    ftest.write("   force dut.reg_file_.b2v_latch_" + reg + "_hi.we=1;\n")
81
    ftest.write("   force dut.reg_file_.b2v_latch_" + reg + "_lo.db=8'h" + hex[2:] + ";\n")
82
    ftest.write("   force dut.reg_file_.b2v_latch_" + reg + "_hi.db=8'h" + hex[0:2] + ";\n")
83
    ftest.write("#2 release dut.reg_file_.b2v_latch_" + reg + "_lo.we;\n")
84
    ftest.write("   release dut.reg_file_.b2v_latch_" + reg + "_hi.we;\n")
85
    ftest.write("   release dut.reg_file_.b2v_latch_" + reg + "_lo.db;\n")
86
    ftest.write("   release dut.reg_file_.b2v_latch_" + reg + "_hi.db;\n")
87
    total_clks = total_clks + 2
88
 
89
def RegRead(reg, hex):
90
    ftest.write("   if (dut.reg_file_.b2v_latch_" + reg + "_lo.latch!==8'h" + hex[2:] +  ") $fdisplay(f,\"* Reg " + reg + " " + reg[1] + "=%h !=" + hex[2:] +  "\",dut.reg_file_.b2v_latch_" + reg + "_lo.latch);\n")
91
    ftest.write("   if (dut.reg_file_.b2v_latch_" + reg + "_hi.latch!==8'h" + hex[0:2] + ") $fdisplay(f,\"* Reg " + reg + " " + reg[0] + "=%h !=" + hex[0:2] + "\",dut.reg_file_.b2v_latch_" + reg + "_hi.latch);\n")
92
 
93
#---------------------------- START -----------------------------------
94
# Create a file that should be included in the test_fuse source
95
ftest = open('test_fuse.i', 'w')
96
ftest.write("// Automatically generated by genfuse.py\n\n")
97
 
98
# Initial pre-test state is reset and control signals asserted
99
ftest.write("force dut.reg_file_.reg_gp_we=0;\n")
100
ftest.write("force dut.reg_control_.ctl_reg_sys_we=0;\n")
101
ftest.write("force dut.z80_top_ifc_n.fpga_reset=1;\n")
102
ftest.write("#2\n")
103
total_clks = total_clks + 2
104
 
105
# Read each test from the testdat.in file
106
while True:
107
    ftest.write("//" + "-" * 80 + "\n")
108
    if len(t1)==0 or run_tests==0:
109
        break
110
    run_tests = run_tests-1
111
 
112
    # Clear opcode register before starting a new instruction
113
    ftest.write("   force dut.instruction_reg_.ctl_ir_we=1;\n")
114
    ftest.write("   force dut.instruction_reg_.db=0;\n")
115
    ftest.write("#2 release dut.instruction_reg_.ctl_ir_we;\n")
116
    ftest.write("   release dut.instruction_reg_.db;\n")
117
    total_clks = total_clks + 2
118
 
119
    # Format of the test.in file:
120
    # <arbitrary test description>
121
    # AF BC DE HL AF' BC' DE' HL' IX IY SP PC
122
    # I R IFF1 IFF2 IM <halted> <tstates>
123
    name = t1.pop(0)
124
    ftest.write("$fdisplay(f,\"Testing opcode " + name + "\");\n")
125
    name = name.split(" ")[0]
126
    r = t1.pop(0).split(' ')
127
    r = filter(None, r)
128
    # 0  1  2  3  4   5   6   7   8  9  10 11   (index)
129
    # AF BC DE HL AF' BC' DE' HL' IX IY SP PC
130
    RegWrite("af", r[0])
131
    RegWrite("bc", r[1])
132
    RegWrite("de", r[2])
133
    RegWrite("hl", r[3])
134
    RegWrite("af2", r[4])
135
    RegWrite("bc2", r[5])
136
    RegWrite("de2", r[6])
137
    RegWrite("hl2", r[7])
138
    RegWrite("ix", r[8])
139
    RegWrite("iy", r[9])
140
    RegWrite("sp", r[10])
141
    RegWrite("wz", "0000")       # Initialize WZ with 0
142
    RegWrite("pc", r[11])
143
 
144
    s = t1.pop(0).split(' ')
145
    s = filter(None, s)
146
    # 0 1 2    3    4  5        6          (index)
147
    # I R IFF1 IFF2 IM <halted> <tstates?>
148
    RegWrite("ir", s[0]+s[1])
149
    # TODO: Store IFF1/IFF2, IM, in_halt
150
 
151
    # Read memory configuration from the test.in until the line contains only -1
152
    while True:
153
        m = t1.pop(0).split(' ')
154
        if m[0]=="-1":
155
            break
156
        address = int(m.pop(0),16)
157
        ftest.write("   // Preset memory\n")
158
        while True:
159
            d = m.pop(0)
160
            if d=="-1":
161
                break
162
            ftest.write("   ram.Mem[" + str(address) + "] = 8'h" + d + ";\n")
163
            address = address+1
164
 
165
    # We need to prepare the IO map to be able to handle IN/OUT instructions.
166
    # Copy tests.out (so we don't modify it just yet), parse all PR and PW (port read, write)
167
    # statements and then fill in our IO map (for IO reads) or stack the check statements to be
168
    # used below after the opcode has executed (for IO writes)
169
    check_io = []               # List of check statements (for OUT instructions)
170
    t2b = list(t2)
171
    while True:
172
        m = t2b.pop(0).split(' ')
173
        m = filter(None, m)
174
        if len(m)==0 or m[0]=="-1":
175
            break
176
        if len(m)==4 and m[1]=="PR":
177
            address = int(m[2],16)
178
            ftest.write("   io.IO[" + str(address) + "] = 8'h" + m[3] + ";\n")
179
        if len(m)==4 and m[1]=="PW":
180
            address = int(m[2],16)
181
            check_io.append("   if (io.IO[" + str(address) + "]!==8'h" + m[3] + ") $fdisplay(f,\"* IO[" + hex(address)[2:] + "]=%h !=" + m[3] + "\",io.IO[" + str(address) + "]);\n")
182
 
183
    # Prepare instruction to be run. By releasing the fpga_reset, internal CPU reset will be active for 1T.
184
    # Due to the instruction execution overlap, first 2T of an instruction may be writing
185
    # value back to a general purpose register (like AF) and we need to prevent that.
186
    # Similarly, we let the execution continues 2T into the next instruction but we prevent
187
    # it from writing to system registers so it cannot update PC and IR.
188
    ftest.write("   force dut.z80_top_ifc_n.fpga_reset=0;\n")
189
    ftest.write("   force dut.address_latch_.abus=16'h" + r[11] +";\n")
190
    ftest.write("   release dut.reg_control_.ctl_reg_sys_we;\n")
191
    ftest.write("   release dut.reg_file_.reg_gp_we;\n")
192
    ftest.write("#3\n")             # 1T (#2) overlaps the reset cycle
193
    total_clks = total_clks + 3     # We borrow 1T (#2) to to force the PC to be what our test wants...
194
    ftest.write("   release dut.address_latch_.abus;\n")
195
    ftest.write("#1\n")
196
    total_clks = total_clks + 1
197
 
198
    # Read and parse the tests expected list which contains the expected results of our run,
199
    # including the number of clocks for a particular instruction
200
    xname = t2.pop(0).split()[0]
201
    if name!=xname:
202
        print("Test " + name + " does not correspond to test.expected " + xname)
203
        break
204
    # Skip the memory access logs; read to the expected register content list
205
    while True:
206
        l = t2.pop(0)
207
        if l[0]!=' ':
208
            break
209
    r = l.split(' ')
210
    r = filter(None, r)
211
 
212
    s = t2.pop(0).split(' ')
213
    s = filter(None, s)
214
 
215
    ticks = int(s[6]) * 2 - 2       # We return 1T (#2) that we borrowed to set PC
216
    total_clks = total_clks + ticks
217
    ftest.write("#" + str(ticks) + " // Execute\n")
218
 
219
    ftest.write("   force dut.reg_control_.ctl_reg_sys_we=0;\n")
220
    ftest.write("#2 pc=z.A;\n")     # Extra 2T for the next instruction overlap & read PC on the ABus
221
    ftest.write("#2\n")             # Complete this instruction
222
    ftest.write("#1 force dut.reg_file_.reg_gp_we=0;\n")    # Add 1/2 clock for any pending flops to latch (mainly the F register)
223
    ftest.write("   force dut.z80_top_ifc_n.fpga_reset=1;\n")
224
    total_clks = total_clks + 5
225
 
226
    # Now we can issue register reading commands
227
    # We are guided on what to read and check by the content of "test.expected" file
228
 
229
    # Special case are the register exchange instructions and there are 3 of them.
230
    # The exchange operations are not tested directly; instead, the latches that control register bank access are
231
    if xname=="08":                 # EX AF,AF1
232
        r[0],r[4] = r[4],r[0]
233
        ftest.write("   if (dut.reg_control_.bank_af!==1) $fdisplay(f,\"* Bank AF!=1\");\n")
234
    if xname=="eb":                 # EX DE,HL
235
        r[2],r[3] = r[3],r[2]
236
        ftest.write("   if (dut.reg_control_.bank_hl_de1!==1) $fdisplay(f,\"* Bank HL/DE!=1\");\n")
237
    if xname=="d9":                 # EXX
238
        r[1],r[5] = r[5],r[1]
239
        r[2],r[6] = r[6],r[2]
240
        r[3],r[7] = r[7],r[3]
241
        ftest.write("   if (dut.reg_control_.bank_exx!==1) $fdisplay(f,\"* Bank EXX!=1\");\n")
242
 
243
    # Read the result: registers and memory
244
    # 0  1  2  3  4   5   6   7   8  9  10 11   (index)
245
    # AF BC DE HL AF' BC' DE' HL' IX IY SP PC
246
    RegRead("af", r[0])
247
    RegRead("bc", r[1])
248
    RegRead("de", r[2])
249
    RegRead("hl", r[3])
250
    RegRead("af2", r[4])
251
    RegRead("bc2", r[5])
252
    RegRead("de2", r[6])
253
    RegRead("hl2", r[7])
254
    RegRead("ix", r[8])
255
    RegRead("iy", r[9])
256
    RegRead("sp", r[10])
257
    #RegRead("pc", r[11]) Instead of PC, we read the address bus of the next instruction
258
    ftest.write("   if (pc!==16'h" + r[11] +  ") $fdisplay(f,\"* PC=%h !=" + r[11] +  "\",pc);\n")
259
 
260
    # 0 1 2    3    4  5        6          (index)
261
    # I R IFF1 IFF2 IM <halted> <tstates?>
262
    RegRead("ir", s[0]+s[1])
263
 
264
    # Read memory configuration until an empty line or -1 at the end
265
    while True:
266
        m = t2.pop(0).split(' ')
267
        m = filter(None, m)
268
        if len(m)==0 or m[0]=="-1":
269
            break
270
        address = int(m.pop(0),16)
271
        while True:
272
            d = m.pop(0)
273
            if d=="-1":
274
                break
275
            ftest.write("   if (ram.Mem[" + str(address) + "]!==8'h" + d + ") $fdisplay(f,\"* Mem[" + hex(address)[2:] + "]=%h !=" + d + "\",ram.Mem[" + str(address) + "]);\n")
276
            address = address+1
277
    # Read a list of IO checks that was compiled while parsing the initial condition
278
    while len(check_io)>0:
279
        ftest.write(check_io.pop(0))
280
 
281
# Write out the total number of clocks that this set of tests takes to execute
282
ftest.write("`define TOTAL_CLKS " + str(total_clks) + "\n")
283
ftest.write("$fdisplay(f,\"=== Tests completed ===\");\n")
284
 
285
# Touch a file that includes 'test_fuse.i' to ensure it will recompile correctly
286
os.utime("test_fuse.sv", None)

powered by: WebSVN 2.1.0

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