NathanY3G / rp2040-pio-emulator

RP2040 emulator for the testing and debugging of PIO programs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

'wrap_target' is hardcoded to 0

Josverl opened this issue · comments

In testing a PIO program that changes the wrap_target by setting the wrap_target label somewhere else than the start of the PIO program, I found that the emulator has this position hard-coded

if not stalled:
current_state = _advance_program_counter(
instruction, condition_met, 0, wrap_top, current_state
)

to support emulating an pio program like:

@asm_pio()
def dmx_receive():
    """PIO program to receive a DMX Universe frame of 512 channels."""
    # Constants
    dmx_bit = 4  # As DMX has a baudrate of 250.000kBaud, a single bit is 4us

    # Break loop
    # Receiver DMX break signal is 88us, so we need to loop 22 times to get 88us
    label("break_reset")
    set(x, 29)                          # 0

    label("break_loop")                 # BREAK = low for 88us
    jmp(pin, "break_reset")             # 1 | Go back to start if pin goes high during BREAK
    jmp(x_dec, "break_loop")        [1] # 2 | wait until BREAK time over (22 loops * 4us = 88us)
    
    wait(1, pin, 0)                     # 3 | wait for the Mark-After-Break (MAB)

    # Data loop
    # First start bit   - no need to detect end of frame
    label("wrap_target")                    # Start of a byte
    wait(0, pin, 0)                 [1] # 4 | Wait for START bit (low) + 1+1us - measure halfway through the bit
    set(x, 7)                       [3] # 5 | 7 more bit;  skip to halfway first bit

    label("bitloop")
    in_(pins, 1)                    [1] # 6 Shift data bit into ISR
    jmp(x_dec, "bitloop")           [1] # 7 Loop 8 times, each loop iteration is 4us

    # Stop bits
    wait(1, pin, 0)                     # 8 Wait for pin to go high for stop bit-1
    set(x, 7)                       [2] # get ready for a next byte
    wait(1, pin, 0)                 [1] # 9 Wait for pin to go high for stop bit-2
    # TODO check if STOP bits are at 8us long
    # if longer than 8 us then we are at the end of the frame  MARK Time after Slot 512
    # this can be up to  1 second - which may be too long for a PIO program to count
    push()     

currently @ clock = 227 this wraps back to program_counter = 0 in stead of program_counter = 4 as show in the below trace:

Clk, PC,      GPIO ->      GPIO,        X, ISR.....(l)
210,  6,         0 ->         0        11 0                                   5 
211,  7,         0 ->         0        10 0                                   5 
214,  6,         0 ->         0        10 0                                   6 
215,  7,         0 ->         0         1 0                                   6 
218,  6,         0 ->         0         1 0                                   7 
219,  7,         0 ->         0         0 0                                   7 
222,  6,         0 ->         0         0 0                                   8 
223,  7,         0 ->         0        -1 0                                   8 
226,  8,         0 ->         1        -1 0                                   8 
227,  9,         1 ->         1        -1                                     0 
228,  0,         1 ->         1    1_1101                                     0 
229,  1,         1 ->         1    1_1101                                     0 
230,  2,         1 ->         1    1_1100                                     0 
232,  1,         1 ->         0    1_1100                                     0 
233,  2,         0 ->         0    1_1011                                     0 
235,  1,         0 ->         0    1_1011                                     0 

It would be good if the wrap_target opcode # could be set in the emulator's constructor

    emu = emulate(
        pio_code,
        stop_when=detect_startbit,
        initial_state=state,
        input_source=incoming_signals,
        jmp_pin=0b0000_0001,
        wrap_target = 4
    )

Thanks for contributing @Josverl - I will pick the MR up after work.