tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.44k stars 911 forks source link

"time" appears completely useless for target gameboy-advance #1578

Closed xen0bit closed 3 years ago

xen0bit commented 3 years ago
package main

// Draw a red square on the GameBoy Advance screen after waiting for a period of time

import (
    "image/color"
    "machine"
    "time"
)

var display = machine.Display

func main() {
    display.Configure()

    //Try to sleep with function
    time.Sleep(10 * time.Second)
    //Try to sleep with while
    now := time.Now().Unix()
    for (time.Now().Unix() - now) < 1000000 {
        _ = 0
    }

    //Draw a red square
    for x := int16(30); x < 50; x++ {
        for y := int16(80); y < 100; y++ {
            display.SetPixel(x, y, color.RGBA{255, 0, 0, 255})
        }
    }
    display.Display()
}

tinygo build -target gameboy-advance -o .\timetest.gba timetest.go

Expected result: There is some sort of delay before drawing the red square Actual result: The red square is drawn immediately

Tested behavior in m-gba, visualboy advance, iodine, gbajs, and even on a legitimate Gameboy Advance SP using a flash cart.

It seems like I must be missing something obvious because this should be extremely straightforward to perform. Is time just not working for this target?

xen0bit commented 3 years ago

While the "time" import still doesn't seem to be working, you can reference my example code here to make a basic timer:

https://github.com/xen0bit/gbablog/commit/276e29cd699b97a75e4de65bd232d5fba2d3493c

In the example I hook machine.IRQ_VBLANK which fires the interrupt handler roughly 59.71540070833901659232620059483728860926694 times a second. Working backward from that, you can make your own timer.

I may submit a PR to hook the actual IRQ_TIMER0 in a way that "time" will work, but for now this is a valid workaround and I'm closing this. Thanks to all who worked on the interrupts API.