Tutorial · Chapter 12 of 23

Move raw bytes through an REU

Transfer an eight-byte pattern between address spaces and check the byte count before starting DMA.

Jump to a section

Budget each end of the transfer

Think of an REU as a second address space. The CPU reads a C64 RAM buffer through a pointer. The REU controller copies bytes between that buffer and an REU address. The transfer in this lesson starts at $010000 in REU and ends at $9000 in visible C64 RAM.

OwnerAddress and capacityJob
REU pattern$010000–$010007Hold eight bytes of pattern data.
C64 output buffer$9000–$9007Receive the portion the CPU needs.

The highest exclusive REU address is $010008. A 128 KiB expansion covers that address. The presence check is separate from this capacity calculation. Named regions reserve addresses in the compiler's map. A raw DMA call uses the addresses supplied at its call site, so the program must stay within those regions.

Reject zero and oversized requests before the copy

The caller can choose a runtime byte count. A zero count encodes a 64 KiB transfer at the hardware, so return before issuing DMA when the caller requests zero bytes. Also reject a count larger than either buffer's remaining capacity. This example starts at each buffer's base, so both limits are eight.

reu-bounded-copy.zp64
const c64 = @import("c64");
const ZeroPage = @import("ZeroPage");

memory {
    region pattern: reu at 0x010000 size 8;
    region output_row: c64 at 0x9000 size 8;
}

fn fetch(count: u16) bool {
    if (count == 0 or count > 8) {
        return false;
    }
    ZeroPage.reu.copyReuToC64(0x010000, 0x9000, count);
    return true;
}

fn check() bool {
    const output: [*]volatile u8 = @ptrFromInt(0x9000);
    output[0] = 0;
    output[3] = 0;
    ZeroPage.reu.fill(0x010000, 0x55, 8);

    const empty = fetch(0);
    const too_large = fetch(9);
    const stayed_empty = output[0] == 0;
    const copied = fetch(3);
    return empty == false and too_large == false and stayed_empty and
        copied and output[0] == 0x55 and output[2] == 0x55 and output[3] == 0;
}

export fn main() void {
    c64.console.clear();
    if (ZeroPage.reu.present() == false) {
        c64.console.print("ENABLE AN REU\n", .{});
    } else if (check()) {
        c64.console.print("BOUNDED COPY OK\n", .{});
        c64.vic.border = 5;
    } else {
        c64.console.print("BOUNDED COPY FAILED\n", .{});
        c64.vic.border = 2;
    }
}

Open the complete source

The rejected requests for zero and nine bytes leave the destination alone. The request for three bytes copies the pattern. Expect BOUNDED COPY OK. In the VICE monitor, m 9000 9003 should show 55 55 55 00. The fourth byte was outside the transfer.

Choose the direction from the data you need

In the round-trip example, copyC64ToReu saves the source, then copyReuToC64 fetches it into another C64 address. compare checks the two ranges. After a fill in REU, swap exchanges the REU pattern and C64 result. The screen reports REU TRANSFER OK after checking both sides.

Open the complete source

Use raw transfers for a byte pattern or an asset with a known layout. When the program works with fields such as terrain, visit counters, and patrol data, use the typed views in the next two chapters. The REU guide lists the raw operations and their operand limits.