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

Subversion Repositories light52

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

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

powered by: WebSVN 2.1.0

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