github.com/tinygo-org/tinygo@v0.31.3-0.20240404173401-90b0bf646c27/src/runtime/runtime_cortexm_qemu.go (about) 1 //go:build cortexm && qemu 2 3 package runtime 4 5 // This file implements the Stellaris LM3S6965 Cortex-M3 chip as implemented by 6 // QEMU. 7 8 import ( 9 "device/arm" 10 "runtime/volatile" 11 "unsafe" 12 ) 13 14 type timeUnit int64 15 16 var timestamp timeUnit 17 18 //export Reset_Handler 19 func main() { 20 preinit() 21 run() 22 23 // Signal successful exit. 24 exit(0) 25 } 26 27 func ticksToNanoseconds(ticks timeUnit) int64 { 28 return int64(ticks) 29 } 30 31 func nanosecondsToTicks(ns int64) timeUnit { 32 return timeUnit(ns) 33 } 34 35 func sleepTicks(d timeUnit) { 36 // TODO: actually sleep here for the given time. 37 timestamp += d 38 } 39 40 func ticks() timeUnit { 41 return timestamp 42 } 43 44 // UART0 output register. 45 var stdoutWrite = (*volatile.Register8)(unsafe.Pointer(uintptr(0x4000c000))) 46 47 func putchar(c byte) { 48 stdoutWrite.Set(uint8(c)) 49 } 50 51 func getchar() byte { 52 // dummy, TODO 53 return 0 54 } 55 56 func buffered() int { 57 // dummy, TODO 58 return 0 59 } 60 61 func waitForEvents() { 62 arm.Asm("wfe") 63 } 64 65 func abort() { 66 exit(1) 67 } 68 69 func exit(code int) { 70 // Exit QEMU. 71 if code == 0 { 72 arm.SemihostingCall(arm.SemihostingReportException, arm.SemihostingApplicationExit) 73 } else { 74 arm.SemihostingCall(arm.SemihostingReportException, arm.SemihostingRunTimeErrorUnknown) 75 } 76 77 // Lock up forever (should be unreachable). 78 for { 79 arm.Asm("wfi") 80 } 81 }