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

Subversion Repositories light52

[/] [light52/] [trunk/] [vhdl/] [tb/] [light52_tb_pkg.vhdl] - Blame information for rev 2

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

Line No. Rev Author Line
1 2 ja_rd
 
2
library ieee,modelsim_lib;
3
--use ieee.std_logic_1164.all;
4
--use ieee.std_logic_arith.all;
5
--use ieee.std_logic_unsigned.all;
6
use ieee.std_logic_1164.all;
7
use ieee.numeric_std.all;
8
 
9
use work.light52_pkg.all;
10
 
11
use modelsim_lib.util.all;
12
use std.textio.all;
13
use work.txt_util.all;
14
 
15
package light52_tb_pkg is
16
 
17
-- Maximum line size of for console output log. Lines longer than this will be
18
-- truncated.
19
constant CONSOLE_LOG_LINE_SIZE : integer := 1024*4;
20
 
21
type t_addr_array is array(0 to 1) of t_address;
22
constant BRAM_ADDR_LEN : integer := log2(BRAM_SIZE);
23
subtype t_bram_addr is unsigned(BRAM_ADDR_LEN-1 downto 0);
24
 
25
 
26
type t_opcode_cycle_count is record
27
    min :                   natural;    -- Minimum observed cycle count
28
    max :                   natural;    -- Maximum observed cycle count
29
    exe :                   natural;    -- No. times the opcode was executed
30
end record t_opcode_cycle_count;
31
 
32
type t_cycle_count is array(0 to 255) of t_opcode_cycle_count;
33
 
34
type t_log_info is record
35
    acc_input :             t_byte;
36
    load_acc :              std_logic;
37
    a_reg_prev :            t_byte;
38
    update_sp :             std_logic;
39
    sp_reg_prev :           t_byte;
40
    sp :                    t_byte;
41
    rom_size :              natural;
42
 
43
    update_psw_flags :      std_logic_vector(1 downto 0);
44
    psw :                   t_byte;
45
    psw_prev :              t_byte;
46
    psw_update_addr :       t_address;
47
 
48
    inc_dptr :              std_logic;
49
    inc_dptr_prev :         std_logic;
50
    dptr :                  t_address;
51
 
52
    xdata_we :              std_logic;
53
    xdata_vma :             std_logic;
54
    xdata_addr :            t_address;
55
    xdata_wr :              t_byte;
56
    xdata_rd :              t_byte;
57
 
58
    code_addr :             t_address;
59
    pc :                    t_address;
60
    pc_prev :               t_address;
61
    next_pc :               t_address;
62
    pc_z :                  t_addr_array;
63
    ps :                    t_cpu_state;
64
 
65
    bram_we :               std_logic;
66
    bram_wr_addr :          t_bram_addr;
67
    bram_wr_data_p0 :       t_byte;
68
 
69
    sfr_we :                std_logic;
70
    sfr_wr :                t_byte;
71
    sfr_addr :              t_byte;
72
 
73
    -- Observed cycle count for all executed opcodes.
74
    cycles :                t_cycle_count;
75
    last_opcode :           std_logic_vector(7 downto 0);
76
    opcode :                std_logic_vector(7 downto 0);
77
    code_rd :               std_logic_vector(7 downto 0);
78
    cycle_count :           natural;
79
 
80
    -- Console log line buffer --------------------------------------
81
    con_line_buf :         string(1 to CONSOLE_LOG_LINE_SIZE);
82
    con_line_ix :          integer;
83
 
84
    -- Log trigger --------------------------------------------------
85
    -- Enable logging after fetching from a given address -----------
86
    log_trigger_address :   t_address;
87
    log_triggered :         boolean;
88
 
89
end record t_log_info;
90
 
91
function hstr(slv: unsigned) return string;
92
 
93
procedure log_cpu_status(
94
                signal info :   inout t_log_info;
95
                file l_file :   TEXT;
96
                file con_file : TEXT);
97
 
98
procedure log_cpu_activity(
99
                signal clk :    in std_logic;
100
                signal reset :  in std_logic;
101
                signal done :   in std_logic;
102
                mcu :           string;
103
                signal info :   inout t_log_info;
104
                rom_size :      natural;
105
                iname :         string;
106
                trigger_addr :  in t_address;
107
                file l_file :   TEXT;
108
                file con_file : TEXT);
109
 
110
-- Flush console output to log console file (in case the end of the
111
-- simulation caught an unterminated line in the buffer)
112
procedure log_flush_console(
113
                signal info :   in t_log_info;
114
                file con_file : TEXT);
115
 
116
-- Log cycle count data to file.
117
procedure log_cycle_counts(
118
                signal info :   in t_log_info);
119
 
120
end package;
121
 
122
package body light52_tb_pkg is
123
 
124
function hstr(slv: unsigned) return string is
125
begin
126
    return hstr(std_logic_vector(slv));
127
end function hstr;
128
 
129
 
130
procedure log_cpu_status(
131
                signal info :   inout t_log_info;
132
                file l_file :   TEXT;
133
                file con_file : TEXT) is
134
variable bram_wr_addr : unsigned(7 downto 0);
135
variable opc : natural;
136
variable num_cycles : natural;
137
begin
138
 
139
    -- Update the opcode observed cycle counters.
140
    -- For every opcode, we count the cycles from its decode_0 state to the 
141
    -- next fetch_1 state. To this we have to add 2 (one each for states 
142
    -- decode_0 and fetch_1) and we have the cycle count.
143
    -- we store the min and the max because of conditional jumps.
144
    if (info.ps=decode_0) then
145
        info.opcode <= info.last_opcode;
146
        info.cycle_count <= 0;
147
    else
148
        if (info.ps=fetch_1) then
149
            -- In state fetch_1, get the opcode from the bus
150
            info.last_opcode <= info.code_rd;
151
 
152
            if info.opcode/="UUUUUUUU" then
153
                opc := to_integer(unsigned(info.opcode));
154
                num_cycles := info.cycle_count + 2;
155
                if info.cycles(opc).min > num_cycles then
156
                    info.cycles(opc).min <= num_cycles;
157
                end if;
158
                if info.cycles(opc).max < num_cycles then
159
                    info.cycles(opc).max <= num_cycles;
160
                end if;
161
                info.cycles(opc).exe <= info.cycles(opc).exe + 1;
162
            end if;
163
        end if;
164
        info.cycle_count <= info.cycle_count + 1;
165
    end if;
166
 
167
 
168
    bram_wr_addr := info.bram_wr_addr(7 downto 0);
169
 
170
    -- Log writes to IDATA BRAM
171
    if info.bram_we='1' then
172
        print(l_file, "("& hstr(info.pc)& ") ["& hstr(bram_wr_addr) & "] = "&
173
              hstr(info.bram_wr_data_p0) );
174
    end if;
175
 
176
    -- Log writes to SFRs
177
    if info.sfr_we = '1' then
178
        print(l_file, "("& hstr(info.pc)& ") SFR["&
179
              hstr(info.sfr_addr)& "] = "& hstr(info.sfr_wr));
180
    end if;
181
 
182
    -- Log ACC updates.
183
    -- Note we exclude the 'intermediate update' of ACC in DA instruction.
184
    if (info.load_acc='1' and info.ps/=alu_daa_0) then
185
        if info.a_reg_prev /= info.acc_input then
186
            print(l_file, "("& hstr(info.pc)& ") A = "&
187
                hstr(info.acc_input) );
188
         end if;
189
        info.a_reg_prev <= info.acc_input;
190
    end if;
191
 
192
    -- Log XRAM writes
193
    if (info.xdata_we='1') then
194
         print(l_file, "("& hstr(info.pc)& ") <"&
195
                hstr(info.xdata_addr)& "> = "&
196
                hstr(info.xdata_wr) );
197
    end if;
198
 
199
    -- Log SP explicit and implicit updates.
200
    -- At the beginning of each instruction we log the SP change if there is 
201
    -- any. SP changes at different times for different instructions. This way,
202
    -- the log is always done at the end of the instruction execution, as is
203
    -- done in B51.
204
    if (info.ps=fetch_1) then
205
        if info.sp_reg_prev /= info.sp then
206
            print(l_file, "("& hstr(info.pc)& ") SP = "&
207
                hstr(info.sp) );
208
         end if;
209
        info.sp_reg_prev <= info.sp;
210
    end if;
211
 
212
    -- Log DPTR increments
213
    if (info.inc_dptr_prev='1') then
214
         print(l_file, "("& hstr(info.pc)& ") DPTR = "&
215
               hstr(info.dptr) );
216
    end if;
217
    info.inc_dptr_prev <= info.inc_dptr;
218
 
219
    -- Console logging ---------------------------------------------------------
220
    -- TX data may come from the high or low byte (opcodes.s
221
    -- uses high byte, no_op.c uses low)
222
    if info.sfr_we = '1' and info.sfr_addr = X"99" then
223
 
224
        -- UART TX data goes to output after a bit of line-buffering
225
        -- and editing
226
        if info.sfr_wr = X"0A" then
227
            -- CR received: print output string and clear it
228
            print(con_file, info.con_line_buf(1 to info.con_line_ix));
229
            info.con_line_ix <= 1;
230
            info.con_line_buf <= (others => ' ');
231
        elsif info.sfr_wr = X"0D" then
232
            -- ignore LF
233
        else
234
            -- append char to output string
235
            if info.con_line_ix < info.con_line_buf'high then
236
                info.con_line_buf(info.con_line_ix) <=
237
                        character'val(to_integer(info.sfr_wr));
238
                info.con_line_ix <= info.con_line_ix + 1;
239
                --print(str(info.con_line_ix));
240
            end if;
241
        end if;
242
    end if;
243
 
244
    -- Log jumps 
245
    -- FIXME remove internal state dependency
246
    if info.ps = long_jump or
247
       info.ps = lcall_4 or
248
       info.ps = jmp_adptr_0 or
249
       info.ps = ret_3 or
250
       info.ps = rel_jump or
251
       info.ps = cjne_a_imm_2 or
252
       info.ps = cjne_rn_imm_3 or
253
       info.ps = cjne_ri_imm_5 or
254
       info.ps = cjne_a_dir_3 or
255
       info.ps = jrb_bit_4 or
256
       info.ps = djnz_dir_4
257
       then
258
        -- Catch attempts to jump to addresses out of the ROM bounds -- assume
259
        -- mirroring is not expected to be useful in this case.
260
        -- Note it remains possible to just run into uninitialized ROM areas
261
        -- or out of ROM bounds, we're not checking any of that.
262
        assert info.next_pc < info.rom_size
263
        report "Jump to unmapped code address "& hstr(info.next_pc)&
264
               "h at "& hstr(info.pc)& "h. Simulation stopped."
265
        severity failure;
266
 
267
        print(l_file, "("& hstr(info.pc)& ") PC = "&
268
            hstr(info.next_pc) );
269
    end if;
270
 
271
    -- Log PSW implicit updates: first, whenever the PSW is updated, save the PC
272
    -- for later reference...
273
    if (info.update_psw_flags(0)='1' or info.load_acc='1') then
274
        info.psw_update_addr <= info.pc;
275
    end if;
276
    -- ...then, when the PSW change is actually detected, log it along with the
277
    -- PC value we saved before.
278
    -- The PSW changes late in the instruction cycle and we need this trick to
279
    -- keep the logs ordered.
280
    if (info.psw) /= (info.psw_prev) then
281
        print(l_file, "("& hstr(info.psw_update_addr)& ") PSW = "& hstr(info.psw) );
282
        info.psw_prev <= info.psw;
283
    end if;
284
 
285
    -- Stop the simulation if we find an unconditional, one-instruction endless
286
    -- loop. This will not catch multi-instruction endless loops and is only
287
    -- intended to replicate the behavior of B51 and give the SW a means to 
288
    -- cleanly end the simulation.
289
    -- FIXME use some jump signal, not a single state
290
    if info.ps = long_jump and (info.pc = info.next_pc) then
291
        -- Before quitting, optionally log cycle count table to separate file.
292
        log_cycle_counts(info);
293
 
294
        assert false
295
        report "NONE. Endless loop encountered. Simulation terminated."
296
        severity failure;
297
    end if;
298
 
299
    -- Update the address of the current instruction.
300
    -- The easiest way to know the address of the current instruction is to look
301
    -- at the state machine; when in state decode_0, we know that the opcode
302
    -- address was on code_addr bus two cycles earlier.
303
    -- We don't need to track the PC value cycle by cycle, we only need info.pc
304
    -- to be valid when the logs above are executed, and that's always after
305
    -- state decode_0.
306
    info.pc_z(1) <= info.pc_z(0);
307
    info.pc_z(0) <= info.code_addr;
308
    if info.ps = decode_0 then
309
        info.pc_prev <= info.pc;
310
        info.pc <= info.pc_z(1);
311
    end if;
312
 
313
end procedure log_cpu_status;
314
 
315
procedure log_cpu_activity(
316
                signal clk :    in std_logic;
317
                signal reset :  in std_logic;
318
                signal done :   in std_logic;
319
                mcu :           string;
320
                signal info :   inout t_log_info;
321
                rom_size :      natural;
322
                iname :         string;
323
                trigger_addr :  in t_address;
324
                file l_file :   TEXT;
325
                file con_file : TEXT) is
326
 
327
begin
328
 
329
    -- 'Connect' all the internal signals we want to watch to members of 
330
    -- the info record.
331
    init_signal_spy(mcu& "/cpu/alu/"&"acc_input",iname&".acc_input", 0);
332
    init_signal_spy(mcu& "/cpu/alu/"&"load_acc", iname&".load_acc", 0);
333
    init_signal_spy(mcu& "/cpu/update_sp",       iname&".update_sp", 0);
334
    init_signal_spy(mcu& "/cpu/SP_reg",          iname&".sp", 0);
335
    init_signal_spy(mcu& "/cpu/"&"psw",          iname&".psw", 0);
336
    init_signal_spy(mcu& "/cpu/"&"update_psw_flags",iname&".update_psw_flags(0)", 0);
337
    init_signal_spy(mcu& "/cpu/"&"code_addr",    iname&".code_addr", 0);
338
    init_signal_spy(mcu& "/cpu/"&"ps",           iname&".ps", 0);
339
    init_signal_spy(mcu& "/cpu/"&"bram_we",      iname&".bram_we", 0);
340
    init_signal_spy(mcu& "/cpu/"&"bram_addr_p0", iname&".bram_wr_addr", 0);
341
    init_signal_spy(mcu& "/cpu/"&"bram_wr_data_p0",  iname&".bram_wr_data_p0", 0);
342
    init_signal_spy(mcu& "/cpu/"&"next_pc",      iname&".next_pc", 0);
343
    init_signal_spy(mcu& "/cpu/"&"sfr_we",       iname&".sfr_we", 0);
344
    init_signal_spy(mcu& "/cpu/"&"sfr_wr",       iname&".sfr_wr", 0);
345
    init_signal_spy(mcu& "/cpu/"&"sfr_addr",     iname&".sfr_addr", 0);
346
    init_signal_spy(mcu& "/cpu/"&"inc_dptr",     iname&".inc_dptr", 0);
347
    init_signal_spy(mcu& "/cpu/"&"DPTR_reg",     iname&".dptr", 0);
348
    init_signal_spy(mcu& "/"&"xdata_we",         iname&".xdata_we", 0);
349
    init_signal_spy(mcu& "/"&"xdata_vma",        iname&".xdata_vma", 0);
350
    init_signal_spy(mcu& "/"&"xdata_addr",       iname&".xdata_addr", 0);
351
    init_signal_spy(mcu& "/"&"xdata_wr",         iname&".xdata_wr", 0);
352
    init_signal_spy(mcu& "/cpu/"&"code_rd",      iname&".code_rd", 0);
353
 
354
    -- We force both 'rdy' uart outputs to speed up the simulation (since the
355
    -- UART operation is not simulated by B51, just logged).
356
    signal_force(mcu&"/uart/rx_rdy_flag", "1", 0 ms, freeze, -1 ms, 0);
357
    signal_force(mcu&"/uart/tx_busy", "0", 0 ms, freeze, -1 ms, 0);
358
    -- And we force the UART RX data to a predictable value until we implement
359
    -- UART RX simulation in the B51 simulator, eventually.
360
    signal_force(mcu&"/uart/rx_buffer", "00000000", 0 ms, freeze, -1 ms, 0);
361
 
362
 
363
    -- Initialize the console log line buffer...
364
    info.con_line_buf <= (others => ' ');
365
    -- ...and take note of the ROM size 
366
    -- FIXME this should not be necessary, we know the array size already.
367
    info.rom_size <= rom_size;
368
 
369
    -- Initialize the observed cycle counting logic...
370
    info.cycles <= (others => (999,0,0));
371
    info.cycle_count <= 0;
372
    info.last_opcode <= "UUUUUUUU";
373
 
374
    -- ...and we're ready to start monitoring the system
375
    while done='0' loop
376
        wait until clk'event and clk='1';
377
        if reset='1' then
378
            -- Initialize some aux vars so as to avoid spurious 'diffs' upon
379
            -- reset.
380
            info.pc <= X"0000";
381
            info.psw_prev <= X"00";
382
            info.sp_reg_prev <= X"07";
383
            info.a_reg_prev <= X"00";
384
 
385
            -- Logging must be enabled from outside by setting 
386
            -- log_trigger_address to a suitable value.
387
            info.log_trigger_address <= trigger_addr;
388
            info.log_triggered <= false;
389
 
390
            info.con_line_ix <= 1; -- uart log line buffer is empty
391
        else
392
            log_cpu_status(info, l_file, con_file);
393
        end if;
394
    end loop;
395
 
396
    -- Once finished, optionally log the cycle count table to a separate file.
397
    log_cycle_counts(info);
398
 
399
end procedure log_cpu_activity;
400
 
401
 
402
 
403
procedure log_flush_console(
404
                signal info :   in t_log_info;
405
                file con_file : TEXT) is
406
variable l : line;
407
begin
408
    -- If there's any character in the line buffer...
409
    if info.con_line_ix > 1 then
410
        -- ...then write the line buffer to the console log file.
411
        write(l, info.con_line_buf(1 to info.con_line_ix));
412
        writeline(con_file, l);
413
    end if;
414
end procedure log_flush_console;
415
 
416
procedure log_cycle_counts(
417
                signal info :   in t_log_info) is
418
variable cc : t_opcode_cycle_count;
419
variable opc : natural;
420
file log_cycles_file: TEXT open write_mode is "cycle_count_log.csv";
421
begin
422
 
423
    for row in 0 to 15 loop
424
        for col in 0 to 15 loop
425
            opc := col*16 + row;
426
            cc := info.cycles(opc);
427
            print(log_cycles_file,
428
                  ""& hstr(to_unsigned(opc,8))& ","&
429
                  str(cc.min)& ","& str(cc.max)& ","& str(cc.exe));
430
        end loop;
431
    end loop;
432
 
433
end procedure log_cycle_counts;
434
 
435
 
436
end package body;

powered by: WebSVN 2.1.0

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