|
| 1 | +// +build nintendoswitch |
| 2 | + |
| 3 | +package runtime |
| 4 | + |
| 5 | +type timeUnit int64 |
| 6 | + |
| 7 | +const asyncScheduler = false |
| 8 | + |
| 9 | +func postinit() {} |
| 10 | + |
| 11 | +// Entry point for Go. Initialize all packages and call main.main(). |
| 12 | +//export main |
| 13 | +func main() int { |
| 14 | + preinit() |
| 15 | + run() |
| 16 | + |
| 17 | + // Call exit to correctly finish the program |
| 18 | + // Without this, the application crashes at start, not sure why |
| 19 | + return exit(0) |
| 20 | +} |
| 21 | + |
| 22 | +// sleepTicks sleeps for the specified system ticks |
| 23 | +func sleepTicks(d timeUnit) { |
| 24 | + sleepThread(uint64(ticksToNanoseconds(d))) |
| 25 | +} |
| 26 | + |
| 27 | +// armTicksToNs converts cpu ticks to nanoseconds |
| 28 | +// Nintendo Switch CPU ticks has a fixed rate at 19200000 |
| 29 | +// It is basically 52 ns per tick |
| 30 | +// The formula 625 / 12 is equivalent to 1e9 / 19200000 |
| 31 | +func ticksToNanoseconds(tick timeUnit) int64 { |
| 32 | + return int64(tick * 625 / 12) |
| 33 | +} |
| 34 | + |
| 35 | +func nanosecondsToTicks(ns int64) timeUnit { |
| 36 | + return timeUnit(12 * ns / 625) |
| 37 | +} |
| 38 | + |
| 39 | +func ticks() timeUnit { |
| 40 | + return timeUnit(ticksToNanoseconds(timeUnit(getArmSystemTick()))) |
| 41 | +} |
| 42 | + |
| 43 | +var stdoutBuffer = make([]byte, 0, 120) |
| 44 | + |
| 45 | +func putchar(c byte) { |
| 46 | + if c == '\n' || len(stdoutBuffer)+1 >= 120 { |
| 47 | + NxOutputString(string(stdoutBuffer)) |
| 48 | + stdoutBuffer = stdoutBuffer[:0] |
| 49 | + return |
| 50 | + } |
| 51 | + |
| 52 | + stdoutBuffer = append(stdoutBuffer, c) |
| 53 | +} |
| 54 | + |
| 55 | +func usleep(usec uint) int { |
| 56 | + sleepThread(uint64(usec) * 1000) |
| 57 | + return 0 |
| 58 | +} |
| 59 | + |
| 60 | +func abort() { |
| 61 | + for { |
| 62 | + exit(1) |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +//export sleepThread |
| 67 | +func sleepThread(nanos uint64) |
| 68 | + |
| 69 | +//export exit |
| 70 | +func exit(code int) int |
| 71 | + |
| 72 | +//export armGetSystemTick |
| 73 | +func getArmSystemTick() int64 |
0 commit comments