diff --git a/README-baochip.md b/README-baochip.md index df251a521..43485a8e0 100644 --- a/README-baochip.md +++ b/README-baochip.md @@ -5,6 +5,10 @@ Baochip-based boards use the `bao1x` SoC. There are two boards supported out-of- - `dabao` is a minimal SoM-style breakout board. Basically just the chip on a board. - `baosec` is a hardware security token. It features a camera, display, external memory, buttons, and a supplemental hardware TRNG. +The Baochip bootloader assumes an environment where USB is available. The device will enumerate as a USB mass storage device. Firmware is delivered by copying [`UF2`]((https://makecode.com/blog/one-chip-to-flash-them-all)) files into the drive (it is *not* usable as a generic storage device). Be sure to cleanly unmount or `sync` the drive before booting the device. + +The bootloader also makes a "serial console" available over USB. This is the primary method for developers to interact with the device. Read more about [serial consoles](./README-consoles.md). + The build idiom is `cargo xtask `, i.e. `cargo xtask dabao`. The build will generate a [`UF2`](https://makecode.com/blog/one-chip-to-flash-them-all) artifact, which can be found in `target/riscv32imac-unknown-[xous|none]-elf/release/`. You will need to copy all three artifacts generated (loader.uf2, xous.uf2, and apps.uf2) initially to ensure that the loader, kernel, and applications are at the same revision. After that point if the loader and kernel are not updated, one can just update apps.uf2. Holding down the `PROG` button while plugging the device into USB will cause it to enter a bootloader that enumerates a mass storage device. The build artifacts can then be copied onto the device. Pressing `PROG` again will cause the device to run the program. Targets also enumerate a serial port over USB, which will activate a debug console. @@ -50,11 +54,11 @@ You can fetch pre-built verions of the .uf2 files from the CI pipeline [here](ht ### Applications -Three application targets are supported by Xous: +Three application targets are supported by Xous. Click the target names in this list to see a list of demonstration applications: -- `baremetal` is an unsecured, bare-iron environment. It is `no-std`, but comes with `alloc` pre-initialized and a USB serial console. -- `dabao` is a Xous environment. It boots a full kernel, supports `std`, and features "detached-apps", i.e., users can develop stand-alone applications that run on the OS without having to touch the kernel image. -- `baosec` is a Xous environment. It is like `dabao`, but supports swap memory. As a result it can run much larger, more complicated applications. The on-chip RRAM is reserved for the kernel, while the off-chip swap contains user applications. +- [`baremetal`](./baremetal/) is an unsecured, bare-iron environment. It is `no-std`, but comes with `alloc` pre-initialized and a USB serial console. +- [`dabao`](./apps-dabao/) is a Xous environment. It boots a full kernel, supports `std`, and features "detached-apps", i.e., users can develop stand-alone applications that run on the OS without having to touch the kernel image. +- [`baosec`](./apps-baosec/) is a Xous environment. It is like `dabao`, but supports swap memory. As a result it can run much larger, more complicated applications. The on-chip RRAM is reserved for the kernel, while the off-chip swap contains user applications. Code is delivered via `boot1`. `boot1` is entered by holding down a button while power the device on. The device will show up as a USB mass storage device, at which point a .uf2 file containing the application image is copied to the device, and the update is applied. diff --git a/README-consoles.md b/README-consoles.md new file mode 100644 index 000000000..97515e36c --- /dev/null +++ b/README-consoles.md @@ -0,0 +1,69 @@ +# Serial Consoles + +Serial consoles are the primary method for interacting with devices and the Xous kernel. It operates over either a physical or virtual [serial port](https://en.wikipedia.org/wiki/Serial_port). Once you have set up a serial console, you will be able to talk to the various aspects of your chip through a shell-like [REPL](https://en.wikipedia.org/wiki/Read%E2%80%93eval%E2%80%93print_loop) mechanism. + +## Baochip-1x Serial Console + +Baochip relies primarily upon a "virtual serial console", which is a [USB CDC-ACM](https://en.wikipedia.org/wiki/USB_communications_device_class) device. In short, when you plug a Baochip into your computer, you will get something similar to either a COM port (on windows) or a tty node (on Linux). + +A limitation of the virtual serial console is that if the system crashes during boot, you can't see the output. As a backup, there is a physical serial console also allocated by default by the bootloader. One can access these through pads PB13 (Rx: accepts data from your computer) and PB14 (Tx: sends data to your computer). + +Access requires a physical serial cable with the following characteristics: + +- Signal levels at VDDIO (maybe 1.8V or 3.3V; on `dabao` it is 3.3V) +- Supports 1,000,000 N-8-1 comm rates + +Baochip-1x is tested using [Genuine FTDI serial cables](https://ftdichip.com/products/ttl-232r-rpi/). Note that some USB serial adapters, such as those from Olimex, can *not* support the 1,000,000 baud rate. + +## Precursor Serial Console + +Precursor supports a physical serial console which runs at 115200 N-8-1. It has an internal multiplexer that allows one pair of wires to be selected between the kernel, console, and application serial ports. + +- kernel is where kernel messages & panics are printed +- console is the primary user port. It presents a shell-like environment +- application is an unassigned "free" serial port, usable by applications for debugging or other functionality + +The Precursor serial port is accessible via the debug cable on the Raspberry Pi HAT, using the built-in serial port of the Raspberry Pi SoC. + +## How to Interact with a Serial Console + +Connecting to a serial console requires a helper program in your host environment. + +### Windows + +[putty](https://www.chiark.greenend.org.uk/~sgtatham/putty/latest.html) is a typical Windows-based serial console client. The hardest part is discovering your COM port. To find it, start the device manager (Windows-X -> Device Manager) and look for an entry labeled "Ports (COM &LPT)". If there is just one COM entry there, you're done, that's your COM port. + +If there is more than one COM entry, unplug the cable you intend to connect to. The entry that disappeared is the one you want to use. + +Inside PuTTY, select the "Serial" option for "Connection type", and enter your COM port, followed by a speed of 1000000. Once you click on "open", you should be able to interact with the device. Note that for virtual USB-serial devices, it disconnects every time you reboot the device, or transition between boot and kernel stages. If you need to observe the transition between these states, you must use a physical serial cable. + +### Linux + +There are many ways to talk to serial ports in Linux: `minicom`, `screen`, `picocom`, `cu` - it varies by distribution. If none of the options listed here work for you, please refer to your distribution's maintainers for guidance on how to talk to a serial port. + +`screen` is the method used to test Xous interactions and `pyserial` is the method used to automate interactions. + +Under Ubuntu/debian-like distributions, you can install `screen` with `apt install screen`. Then, add your user to the `dialout` group so you can access the serial port without relying on `sudo`: + +`sudo usermod -aG dialout $USER` + +You will need to log out and log back in again for this to take effect. Some distros may require you to install a custom `udev` rule. If you encounter permission issues, try prefacing your screen command with `sudo`. + +Determine which device node the device is plugged into using `dmesg`. Look for an entry like this: + +``` +[180822.853542] usb 1-1.1: Manufacturer: Baochip +[180822.853552] usb 1-1.1: SerialNumber: HV92R0 +[180822.864377] hid-generic 0003:1D50:6198.0045: hiddev96,hidraw0: USB HID v1.11 Device [Baochip Baosec-lite] on usb-0000:01:00.0-1.1/input0 +[180822.867022] input: Baochip Baosec-lite as /devices/platform/scb/fd500000.pcie/pci0000:00/0000:00:00.0/0000:01:00.0/usb1/1-1/1-1.1/1-1.1:1.1/0003:1D50:6198.0046/input/input38 +[180823.016898] hid-generic 0003:1D50:6198.0046: input,hidraw1: USB HID v1.11 Keyboard [Baochip Baosec-lite] on usb-0000:01:00.0-1.1/input1 +[180823.019625] cdc_acm 1-1.1:1.2: ttyACM0: USB ACM device +``` + +Here, `ttyACM0` is the device node. Connect to it using `screen /dev/ttyACM0 1000000`. + +`screen` requires control sequences to exit the terminal. By default it is `control-A \`, as in, hold ctrl+A, and then hit `\`. It will ask you if you want to exit screen, and respond with `y` to exit. + +Note that `screen` will leave zombies around if there is an ungraceful exit for any reason. These are zombie processes that fight for control over the serial port and lead to extremely confusing behavior. If your terminal goes unresponsive to input or your output looks garbled, check for zombies. + +To check for such zombies, run `ps aux | grep -i screen`. If you see any entries other than the `grep` entry, then you may have to kill the zombie. Either kill it using its process ID, or you can run `sudo pkill -i screen`. The `-i` is particularly important because often times the zombie is called `SCREEN`, which needs to be killed with `sudo` and must be either typed in exact case or referred to with the `-i` option. diff --git a/libs/bao1x-hal/src/bio_hw.rs b/libs/bao1x-hal/src/bio_hw.rs index adb6e63bf..fca4086ef 100644 --- a/libs/bao1x-hal/src/bio_hw.rs +++ b/libs/bao1x-hal/src/bio_hw.rs @@ -3,10 +3,7 @@ use arbitrary_int::Number; use bao1x_api::IoxHal; use bao1x_api::{bio::*, bio_code}; use utra::bio_bdma; -use utralib::{ - utra::bio_bdma::{SFR_ELEVEL_FIFO_EVENT_LEVEL0, SFR_IRQMASK_0}, - *, -}; +use utralib::{utra::bio_bdma::*, *}; #[cfg(not(feature = "std"))] use crate::iox::Iox; @@ -624,15 +621,16 @@ impl<'a> BioApi<'a> for BioSharedState { fn setup_fifo_event_triggers(&mut self, config: FifoEventConfig) -> Result<(), BioError> { let event_offset = config.which.to_usize_checked() * 2 + config.trigger_slot.raw_value() as usize; - assert!(event_offset <= 7, "Computed event offset is invalid"); - // safety: this is safe because which is bounds checked with to_usize_checked() and the - // implementation of arbitrary_int that encodes trigger_slot is also bounds checked. The assert - // above also helps confirm a lack of logic bugs. - unsafe { - self.bio - .base() - .add(SFR_ELEVEL_FIFO_EVENT_LEVEL0.offset() + event_offset) - .write_volatile(config.level.level().as_u32()); + match event_offset { + 0 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL0, config.level.level().as_u32()), + 1 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL1, config.level.level().as_u32()), + 2 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL2, config.level.level().as_u32()), + 3 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL3, config.level.level().as_u32()), + 4 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL4, config.level.level().as_u32()), + 5 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL5, config.level.level().as_u32()), + 6 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL6, config.level.level().as_u32()), + 7 => self.bio.rmwf(SFR_ELEVEL_FIFO_EVENT_LEVEL7, config.level.level().as_u32()), + _ => unimplemented!("Computed event offset is invalid"), } let mask = 1u32 << event_offset as u32; let mut lt_gt_eq_set = 0u32; diff --git a/libs/bio-lib/src/c/colorwheel/colorwheel.rs b/libs/bio-lib/src/c/colorwheel/colorwheel.rs index bf9071ccf..ce6169cea 100644 --- a/libs/bio-lib/src/c/colorwheel/colorwheel.rs +++ b/libs/bio-lib/src/c/colorwheel/colorwheel.rs @@ -1,4 +1,4 @@ -// AUTOGENERATED CODE on 2026-02-22T03:16:34.024624 - do not edit directly! +// AUTOGENERATED CODE on 2026-03-20T22:34:23.718174 - do not edit directly! use bao1x_api::bio_code_aligned; @@ -7,11 +7,56 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "20:", // _start: @_start // --- inline asm begin --- "lui sp, 1", - "j 74f", + "j 80f", // --- inline asm end --- - "21:", // ws2812c: @ws2812c + "21:", // fp_div: @fp_div + "lui a2, 524288", + "beqz a1, 26f", + "srai a3, a0, 31", + "srai a4, a1, 31", + "xor a5, a0, a3", + "sub t2, a5, a3", + "xor a5, a1, a4", + "sub a4, a5, a4", + "lui a5, 16", + "bgeu a4, t2, 23f", + "22:", // .LBB1_2: =>This Inner Loop Header: Depth=1 + "slli a4, a4, 1", + "slli a5, a5, 1", + "bltu a4, t2, 22b", + "beqz a5, 26f", + "23:", // .LBB1_4 + "srli t0, a4, 31", + "srl t1, a4, t0", + "li a4, 0", + "beqz a0, 25f", + "srl a5, a5, t0", + "li t0, 2", + "24:", // .LBB1_6: =>This Inner Loop Header: Depth=1 + "sltu a2, t2, t1", + "addi a2, a2, -1", + "and a3, a2, t1", + "and a2, a2, a5", + "sub t2, t2, a3", + "or a4, a4, a2", + "slli t2, t2, 1", + "bltu a5, t0, 25f", + // in Loop: Header=BB1_6 Depth=1 + "srli a5, a5, 1", + "bnez t2, 24b", + "25:", // .LBB1_8 + "sltu a2, t2, t1", + "xori a2, a2, 1", + "xor a0, a0, a1", + "add a2, a2, a4", + "bgez a0, 26f", + "neg a2, a2", + "26:", // .LBB1_10 + "mv a0, a2", + "ret", + "27:", // ws2812c: @ws2812c "li a3, 31", - "bltu a3, a0, 26f", + "bltu a3, a0, 32f", "li a3, 1", "sll t1, a3, a0", "not a3, t1", @@ -27,15 +72,15 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI // --- inline asm begin --- "li x20, 0", // --- inline asm end --- - "beqz a2, 26f", + "beqz a2, 32f", "li t0, 0", - "22:", // .LBB1_3: =>This Loop Header: Depth=1 - // Child Loop BB1_4 Depth 2 + "28:", // .LBB2_3: =>This Loop Header: Depth=1 + // Child Loop BB2_4 Depth 2 "slli a4, t0, 2", "add a4, a4, a1", "lw a0, 0(a4)", "li a4, 24", - "23:", // .LBB1_4: Parent Loop BB1_3 Depth=1 + "29:", // .LBB2_4: Parent Loop BB2_3 Depth=1 // => This Inner Loop Header: Depth=2 // --- inline asm begin --- "mv x22, t1", @@ -47,8 +92,8 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "li x20, 0", // --- inline asm end --- "slli a5, a0, 8", - "bltz a5, 24f", - // in Loop: Header=BB1_4 Depth=2 + "bltz a5, 30f", + // in Loop: Header=BB2_4 Depth=2 // --- inline asm begin --- "mv x23, a3", // --- inline asm end --- @@ -67,8 +112,8 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI // --- inline asm begin --- "li x20, 0", // --- inline asm end --- - "j 25f", - "24:", // .LBB1_6: in Loop: Header=BB1_4 Depth=2 + "j 31f", + "30:", // .LBB2_6: in Loop: Header=BB2_4 Depth=2 // --- inline asm begin --- "li x20, 0", // --- inline asm end --- @@ -96,24 +141,24 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI // --- inline asm begin --- "li x20, 0", // --- inline asm end --- - "25:", // .LBB1_7: in Loop: Header=BB1_4 Depth=2 + "31:", // .LBB2_7: in Loop: Header=BB2_4 Depth=2 "addi a4, a4, -1", "slli a0, a0, 1", - "bnez a4, 23b", - // in Loop: Header=BB1_3 Depth=1 + "bnez a4, 29b", + // in Loop: Header=BB2_3 Depth=1 "addi t0, t0, 1", - "bne t0, a2, 22b", - "26:", // .LBB1_9 + "bne t0, a2, 28b", + "32:", // .LBB2_9 "ret", - "27:", // __udivsi3: @__udivsi3 - "beqz a1, 29f", + "33:", // __udivsi3: @__udivsi3 + "beqz a1, 35f", "mv t2, a0", "li a4, 0", "li a0, 0", "li a3, 31", "li t0, 1", "li t1, -1", - "28:", // .LBB2_2: =>This Inner Loop Header: Depth=1 + "34:", // .LBB3_2: =>This Inner Loop Header: Depth=1 "slli a4, a4, 1", "srl a5, t2, a3", "sll a2, t0, a3", @@ -126,17 +171,17 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "and a5, a5, a1", "or a0, a0, a2", "sub a4, a4, a5", - "bne a3, t1, 28b", + "bne a3, t1, 34b", "ret", - "29:", // .LBB2_4 + "35:", // .LBB3_4 "li a0, -1", "ret", - "30:", // __umodsi3: @__umodsi3 - "beqz a1, 32f", + "36:", // __umodsi3: @__umodsi3 + "beqz a1, 38f", "li a2, 0", "li a3, 31", "li a4, -1", - "31:", // .LBB3_2: =>This Inner Loop Header: Depth=1 + "37:", // .LBB4_2: =>This Inner Loop Header: Depth=1 "slli a2, a2, 1", "srl a5, a0, a3", "andi a5, a5, 1", @@ -146,19 +191,19 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "and a5, a5, a1", "addi a3, a3, -1", "sub a2, a2, a5", - "bne a3, a4, 31b", - "j 33f", - "32:", // .LBB3_3 + "bne a3, a4, 37b", + "j 39f", + "38:", // .LBB4_3 "mv a2, a0", - "33:", // .LBB3_4 + "39:", // .LBB4_4 "mv a0, a2", "ret", - "34:", // __divsi3: @__divsi3 + "40:", // __divsi3: @__divsi3 "addi sp, sp, -16", "sw s0, 12(sp)", // 4-byte Folded Spill "sw s1, 8(sp)", // 4-byte Folded Spill "mv t0, a0", - "beqz a1, 36f", + "beqz a1, 42f", "li a4, 0", "li a0, 0", "srai t2, t0, 31", @@ -170,7 +215,7 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "xor a2, a1, s0", "sub a2, a2, s0", "li t2, -1", - "35:", // .LBB4_2: =>This Inner Loop Header: Depth=1 + "41:", // .LBB5_2: =>This Inner Loop Header: Depth=1 "slli a4, a4, 1", "srl s0, a5, a3", "sll s1, t1, a3", @@ -183,31 +228,31 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "and s0, s0, a2", "or a0, a0, s1", "sub a4, a4, s0", - "bne a3, t2, 35b", - "j 37f", - "36:", // .LBB4_3 + "bne a3, t2, 41b", + "j 43f", + "42:", // .LBB5_3 "li a0, -1", - "37:", // .LBB4_4 + "43:", // .LBB5_4 "xor a1, a1, t0", - "bgez a1, 38f", + "bgez a1, 44f", "neg a0, a0", - "38:", // .LBB4_6 + "44:", // .LBB5_6 "lw s0, 12(sp)", // 4-byte Folded Reload "lw s1, 8(sp)", // 4-byte Folded Reload "addi sp, sp, 16", "ret", - "39:", // __modsi3: @__modsi3 + "45:", // __modsi3: @__modsi3 "srai a2, a0, 31", "xor a3, a0, a2", "sub a3, a3, a2", - "beqz a1, 43f", + "beqz a1, 49f", "li a2, 0", "srai a5, a1, 31", "li a4, 31", "xor a1, a1, a5", "sub a1, a1, a5", "li t0, -1", - "40:", // .LBB5_2: =>This Inner Loop Header: Depth=1 + "46:", // .LBB6_2: =>This Inner Loop Header: Depth=1 "slli a2, a2, 1", "srl a5, a3, a4", "andi a5, a5, 1", @@ -217,22 +262,22 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "and a5, a5, a1", "addi a4, a4, -1", "sub a2, a2, a5", - "bne a4, t0, 40b", - "bgez a0, 42f", - "41:", // .LBB5_4 + "bne a4, t0, 46b", + "bgez a0, 48f", + "47:", // .LBB6_4 "neg a2, a2", - "42:", // .LBB5_5 + "48:", // .LBB6_5 "mv a0, a2", "ret", - "43:", // .LBB5_6 + "49:", // .LBB6_6 "mv a2, a3", - "bltz a0, 41b", - "j 42b", - "44:", // HsvToRgb: @HsvToRgb + "bltz a0, 47b", + "j 48b", + "50:", // HsvToRgb: @HsvToRgb "slli a4, a0, 16", "srli a4, a4, 24", "srli a1, a0, 16", - "beqz a4, 45f", + "beqz a4, 51f", "andi a2, a0, 255", "li a3, 191", "li t0, 213", @@ -260,48 +305,48 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "mul a5, a4, a5", "li a2, 1", "srli a5, a5, 8", - "bge a2, a3, 46f", + "bge a2, a3, 52f", "li a2, 2", - "beq a3, a2, 47f", + "beq a3, a2, 53f", "li a2, 3", - "beq a3, a2, 48f", + "beq a3, a2, 54f", "li a2, 4", - "bne a3, a2, 52f", + "bne a3, a2, 58f", "mv a3, a5", "mv a4, t1", - "j 49f", - "45:", // .LBB6_6 + "j 55f", + "51:", // .LBB7_6 "mv a3, a1", "mv a4, a1", - "j 49f", - "46:", // .LBB6_7 - "beqz a3, 50f", + "j 55f", + "52:", // .LBB7_7 + "beqz a3, 56f", "li a2, 1", - "bne a3, a2, 52f", + "bne a3, a2, 58f", "mv a3, a0", "mv a4, a1", - "j 51f", - "47:", // .LBB6_10 + "j 57f", + "53:", // .LBB7_10 "mv a3, t1", "mv a4, a1", "mv a0, a5", - "j 53f", - "48:", // .LBB6_11 + "j 59f", + "54:", // .LBB7_11 "mv a3, t1", "mv a4, a0", - "49:", // .LBB6_12 + "55:", // .LBB7_12 "mv a0, a1", - "j 53f", - "50:", // .LBB6_13 + "j 59f", + "56:", // .LBB7_13 "mv a3, a1", "mv a4, a5", - "51:", // .LBB6_14 + "57:", // .LBB7_14 "mv a0, t1", - "j 53f", - "52:", // .LBB6_15 + "j 59f", + "58:", // .LBB7_15 "mv a3, a1", "mv a4, t1", - "53:", // .LBB6_16 + "59:", // .LBB7_16 "slli a0, a0, 24", "slli a4, a4, 24", "srli a0, a0, 8", @@ -310,7 +355,7 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "or a1, a1, a4", "or a0, a0, a1", "ret", - "54:", // RgbToHsv: @RgbToHsv + "60:", // RgbToHsv: @RgbToHsv "addi sp, sp, -32", "sw ra, 28(sp)", // 4-byte Folded Spill "sw s0, 24(sp)", // 4-byte Folded Spill @@ -322,85 +367,85 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "sw a1, 12(sp)", // 4-byte Folded Spill "andi a2, a1, 255", "mv a0, s0", - "bgeu s0, s1, 57f", - "bgeu a0, a2, 58f", - "55:", // .LBB7_2 - "bgeu s1, s0, 59f", - "56:", // .LBB7_3 + "bgeu s0, s1, 63f", + "bgeu a0, a2, 64f", + "61:", // .LBB8_2 + "bgeu s1, s0, 65f", + "62:", // .LBB8_3 "mv a1, s0", - "bltu a2, s0, 61f", + "bltu a2, s0, 67f", "mv a1, a2", - "j 61f", - "57:", // .LBB7_5 + "j 67f", + "63:", // .LBB8_5 "mv a0, s1", - "bltu s1, a2, 55b", - "58:", // .LBB7_6 + "bltu s1, a2, 61b", + "64:", // .LBB8_6 "mv a0, a2", - "bltu s1, s0, 56b", - "59:", // .LBB7_7 + "bltu s1, s0, 62b", + "65:", // .LBB8_7 "mv a1, s1", - "bltu a2, s1, 60f", + "bltu a2, s1, 66f", "mv a1, a2", - "60:", // .LBB7_9 - "beqz a1, 64f", - "61:", // .LBB7_10 + "66:", // .LBB8_9 + "beqz a1, 70f", + "67:", // .LBB8_10 "sw a1, 16(sp)", // 4-byte Folded Spill "sub a0, a1, a0", "sw a0, 8(sp)", // 4-byte Folded Spill - "jal ra, 34b", + "jal ra, 40b", "andi a1, a0, 255", - "beqz a1, 62f", + "beqz a1, 68f", "neg a0, a0", "sw a0, 4(sp)", // 4-byte Folded Spill "lw a0, 16(sp)", // 4-byte Folded Reload - "bne a0, s0, 63f", + "bne a0, s0, 69f", "lbu a0, 12(sp)", // 4-byte Folded Reload "sub s1, s1, a0", "lui a0, 688", "mul a0, s1, a0", "srai a0, a0, 16", "lw a1, 8(sp)", // 4-byte Folded Reload - "jal ra, 34b", + "jal ra, 40b", "slli a0, a0, 16", "srli a0, a0, 16", - "j 66f", - "62:", // .LBB7_13 + "j 72f", + "68:", // .LBB8_13 "li a0, 0", - "j 67f", - "63:", // .LBB7_14 - "bne a0, s1, 65f", + "j 73f", + "69:", // .LBB8_14 + "bne a0, s1, 71f", "lbu a0, 12(sp)", // 4-byte Folded Reload "sub a0, a0, s0", "lui a1, 688", "mul a0, a0, a1", "srai a0, a0, 16", "lw a1, 8(sp)", // 4-byte Folded Reload - "jal ra, 34b", + "jal ra, 40b", "slli a0, a0, 16", "srli a0, a0, 16", "addi a0, a0, 85", - "j 66f", - "64:", // .LBB7_16 + "j 72f", + "70:", // .LBB8_16 "li a2, 0", "li a0, 0", - "j 68f", - "65:", // .LBB7_17 + "j 74f", + "71:", // .LBB8_17 "sub s0, s0, s1", "lui a0, 688", "mul a0, s0, a0", "srai a0, a0, 16", "lw a1, 8(sp)", // 4-byte Folded Reload - "jal ra, 34b", + "jal ra, 40b", "slli a0, a0, 16", "srli a0, a0, 16", "addi a0, a0, 171", - "66:", // .LBB7_18 + "72:", // .LBB8_18 "andi a0, a0, 255", "lbu a1, 4(sp)", // 4-byte Folded Reload "slli a1, a1, 8", - "67:", // .LBB7_19 + "73:", // .LBB8_19 "lw a2, 16(sp)", // 4-byte Folded Reload - "68:", // .LBB7_20 + "74:", // .LBB8_20 "slli a2, a2, 16", "or a0, a0, a1", "or a0, a0, a2", @@ -412,7 +457,7 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "addi sp, sp, 4", "addi sp, sp, 32", "ret", - "69:", // rainbow_update: @rainbow_update + "75:", // rainbow_update: @rainbow_update "addi sp, sp, -32", "sw ra, 28(sp)", // 4-byte Folded Spill "sw s0, 24(sp)", // 4-byte Folded Spill @@ -421,25 +466,25 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "li a1, 100", "mv s1, a0", "mv s0, a0", - "bltu a0, a1, 70f", + "bltu a0, a1, 76f", "li s0, 100", - "70:", // .LBB8_2 + "76:", // .LBB9_2 "li a0, 256", "mv a1, s0", - "jal ra, 27b", - "beqz s1, 73f", + "jal ra, 33b", + "beqz s1, 79f", "li a1, 0", - "lui a3, %hi(77f)", + "lui a3, %hi(83f)", "slli a4, s0, 1", - "lw a2, %lo(77f)(a3)", - "addi a3, a3, %lo(77f)", + "lw a2, %lo(83f)(a3)", + "addi a3, a3, %lo(83f)", "mv t0, s0", "add a4, a4, s0", "addi a3, a3, 6", "add a4, a4, a3", "li a5, 200", "li s1, 64", - "71:", // .LBB8_4: =>This Inner Loop Header: Depth=1 + "77:", // .LBB9_4: =>This Inner Loop Header: Depth=1 "mul s0, a1, a0", "add s0, s0, a2", "sb s0, -2(a3)", @@ -447,17 +492,17 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "sb s1, 0(a3)", "addi a3, a3, 3", "addi a1, a1, 1", - "bne a3, a4, 71b", + "bne a3, a4, 77b", "slli a0, t0, 2", - "lui s1, %hi(77f)", - "addi s1, s1, %lo(77f)", + "lui s1, %hi(83f)", + "addi s1, s1, %lo(83f)", "addi s0, s1, 304", "addi s1, s1, 4", "add a0, a0, s0", "sw a0, 12(sp)", // 4-byte Folded Spill // implicit-def: $x10 // kill: killed $x10 - "72:", // .LBB8_6: =>This Inner Loop Header: Depth=1 + "78:", // .LBB9_6: =>This Inner Loop Header: Depth=1 "lbu a0, 1(s1)", "lbu a1, 0(s1)", "lbu a2, 2(s1)", @@ -470,7 +515,7 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "or a1, a1, a2", "or a0, a0, a1", "sw a0, 16(sp)", // 4-byte Folded Spill - "jal ra, 44b", + "jal ra, 50b", "srli a1, a0, 16", "slli a0, a0, 16", "srli a0, a0, 8", @@ -479,14 +524,14 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "addi s0, s0, 4", "addi s1, s1, 3", "lw a0, 12(sp)", // 4-byte Folded Reload - "bne s0, a0, 72b", - "73:", // .LBB8_7 - "lui a0, %hi(77f)", - "lw a1, %lo(77f)(a0)", + "bne s0, a0, 78b", + "79:", // .LBB9_7 + "lui a0, %hi(83f)", + "lw a1, %lo(83f)(a0)", "lw a2, 8(sp)", // 4-byte Folded Reload "add a1, a1, a2", "andi a1, a1, 255", - "sw a1, %lo(77f)(a0)", + "sw a1, %lo(83f)(a0)", "lw ra, 28(sp)", // 4-byte Folded Reload "lw s0, 24(sp)", // 4-byte Folded Reload // ERRATA BUG2 patch: lw s1, 20(sp) (offset=20,bits[4:0]=20) // 4-byte Folded Reload @@ -495,7 +540,7 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "addi sp, sp, 4", "addi sp, sp, 32", "ret", - "74:", // main: @main + "80:", // main: @main "addi sp, sp, -32", "sw ra, 28(sp)", // 4-byte Folded Spill "sw s0, 24(sp)", // 4-byte Folded Spill @@ -513,32 +558,32 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI "sw a0, 12(sp)", // 4-byte Folded Spill "lui a0, 24", "addi s0, a0, 1696", - "75:", // .LBB9_1: =>This Loop Header: Depth=1 - // Child Loop BB9_2 Depth 2 + "81:", // .LBB10_1: =>This Loop Header: Depth=1 + // Child Loop BB10_2 Depth 2 "lw a0, 16(sp)", // 4-byte Folded Reload - "lui a1, %hi(79f)", - "addi a1, a1, %lo(79f)", + "lui a1, %hi(85f)", + "addi a1, a1, %lo(85f)", "mv a2, s1", - "jal ra, 21b", + "jal ra, 27b", "mv a0, s1", "lw a1, 12(sp)", // 4-byte Folded Reload - "jal ra, 69b", + "jal ra, 75b", "mv a0, s0", - "76:", // .LBB9_2: Parent Loop BB9_1 Depth=1 + "82:", // .LBB10_2: Parent Loop BB10_1 Depth=1 // => This Inner Loop Header: Depth=2 // --- inline asm begin --- "li x20, 0", // --- inline asm end --- "addi a0, a0, -1", - "bnez a0, 76b", - "j 75b", + "bnez a0, 82b", + "j 81b", // --- rodata: .L_MergedGlobals (4 entries, 1 words) --- ".p2align 2", - "77:", // .L_MergedGlobals + "83:", // .L_MergedGlobals ".word 0x00000000", // [0] 0, 0, 0, 0 // --- rodata: render_buf (300 entries, 75 words) --- ".p2align 0", - "78:", // render_buf + "84:", // render_buf ".word 0x00000000", // [0] 0, 0, 0, 0 ".word 0x00000000", // [4] 0, 0, 0, 0 ".word 0x00000000", // [8] 0, 0, 0, 0 @@ -616,7 +661,7 @@ bio_code_aligned!(colorwheel_bio_code, BM_COLORWHEEL_BIO_START, BM_COLORWHEEL_BI ".word 0x00000000", // [296] 0, 0, 0, 0 // --- rodata: led_buf (400 entries, 100 words) --- ".p2align 0", - "79:", // led_buf + "85:", // led_buf ".word 0x00000000", // [0] 0, 0, 0, 0 ".word 0x00000000", // [4] 0, 0, 0, 0 ".word 0x00000000", // [8] 0, 0, 0, 0 diff --git a/libs/bio-lib/src/c/include/bio.h b/libs/bio-lib/src/c/include/bio.h index 8282de08b..7ca4e6295 100644 --- a/libs/bio-lib/src/c/include/bio.h +++ b/libs/bio-lib/src/c/include/bio.h @@ -115,6 +115,8 @@ static inline __attribute__((always_inline)) void clear_event_bits(uint32_t mask __asm__ volatile ("mv x29, %0" : : "r"(mask) : "memory"); } +#define FIFO_EVENT_MASK(fifo_number, slot_number) (((1 << slot_number) << (fifo_number * 2)) << 24) + // Debug static inline __attribute__((always_inline)) uint32_t core_id() { uint32_t a; diff --git a/libs/bio-lib/src/c/include/fp_q12.h b/libs/bio-lib/src/c/include/fp_q12.h index 212ecf4d6..631fa64f9 100644 --- a/libs/bio-lib/src/c/include/fp_q12.h +++ b/libs/bio-lib/src/c/include/fp_q12.h @@ -4,6 +4,8 @@ * Format: configurable Q format via FP_FRAC_BITS * Default: Q20.12 (20 integer bits, 12 fractional bits) * + * TODO: division routine is sus. Need to test & revisit. + * * Representation: signed 32-bit integer * Range (Q20.12): -524288.0 to +524287.9998 * Resolution: 1/4096 ~= 0.000244 diff --git a/libs/bio-lib/src/c/include/fp_q16.h b/libs/bio-lib/src/c/include/fp_q16.h index ffb81596e..b8e1cecd4 100644 --- a/libs/bio-lib/src/c/include/fp_q16.h +++ b/libs/bio-lib/src/c/include/fp_q16.h @@ -50,16 +50,59 @@ typedef int32_t fp_t; #define FP_FROM_FLOAT(x) ((fp_t)((x) * FP_ONE + 0.5f)) #define FP_TO_FLOAT(x) ((float)(x) / FP_ONE) +#define FIX16_OVERFLOW 0x80000000 +#define FIX16_MAXIMUM 0x7FFFFFFF /*!< the maximum value of fp_t */ +#define FIX16_MINIMUM 0x80000000 /*!< the minimum value of fp_t */ + /* --- Arithmetic --- */ static inline fp_t fp_add(fp_t a, fp_t b) { - return a + b; + // Use unsigned integers because overflow with signed integers is + // an undefined operation (http://www.airs.com/blog/archives/120). + uint32_t _a = a, _b = b; + uint32_t sum = _a + _b; + + // Overflow can only happen if sign of a == sign of b, and then + // it causes sign of sum != sign of a. + if (!((_a ^ _b) & 0x80000000) && ((_a ^ sum) & 0x80000000)) + return (fp_t) FIX16_OVERFLOW; + + return sum; } static inline fp_t fp_sub(fp_t a, fp_t b) { - return a - b; + uint32_t _a = a, _b = b; + uint32_t diff = _a - _b; + + // Overflow can only happen if sign of a != sign of b, and then + // it causes sign of diff != sign of a. + if (((_a ^ _b) & 0x80000000) && ((_a ^ diff) & 0x80000000)) + return (fp_t) FIX16_OVERFLOW; + + return diff; +} + +/* Saturating arithmetic */ +static inline fp_t fp_sadd(fp_t a, fp_t b) +{ + fp_t result = fp_add(a, b); + + if (result == (fp_t) FIX16_OVERFLOW) + return (a >= 0) ? FIX16_MAXIMUM : FIX16_MINIMUM; + + return result; +} + +static inline fp_t fp_ssub(fp_t a, fp_t b) +{ + fp_t result = fp_sub(a, b); + + if (result == (fp_t) FIX16_OVERFLOW) + return (a >= 0) ? FIX16_MAXIMUM : FIX16_MINIMUM; + + return result; } /* @@ -77,52 +120,75 @@ static inline fp_t fp_mul(fp_t a, fp_t b) return (fp_t)(result >> FP_FRAC_BITS); } -/* - * u32_divrem - unsigned 32-bit shift-and-subtract divider. - * - * 32 iterations of: shift one bit of the dividend into the partial - * remainder, subtract the divisor if it fits, record the quotient bit. - * No hardware divide instruction required. - */ -static inline uint32_t u32_divrem(uint32_t dividend, uint32_t divisor, - uint32_t *remainder) +fp_t fp_div(fp_t a, fp_t b) { - uint32_t quotient = 0; - uint32_t rem = 0; - int i; + // This uses the basic binary restoring division algorithm. + // It appears to be faster to do the whole division manually than + // trying to compose a 64-bit divide out of 32-bit divisions on + // platforms without hardware divide. - for (i = 31; i >= 0; i--) { - rem = (rem << 1) | ((dividend >> i) & 1); - if (rem >= divisor) { - rem -= divisor; - quotient |= (1u << i); - } - } + if (b == 0) + return FIX16_MINIMUM; - if (remainder) *remainder = rem; - return quotient; -} + uint32_t remainder = (a >= 0) ? a : (-a); + uint32_t divider = (b >= 0) ? b : (-b); -/* - * fp_div - signed fixed-point division, no hardware divide. - * - * Works in two passes through u32_divrem: - * pass 1: integer quotient and remainder of |a| / |b| - * pass 2: fractional bits from (remainder << FP_FRAC_BITS) / |b| - * Sign is restored at the end. - */ -static inline fp_t fp_div(fp_t a, fp_t b) -{ - int32_t sign = ((a ^ b) < 0) ? -1 : 1; - uint32_t ua = (uint32_t)(a < 0 ? -a : a); - uint32_t ub = (uint32_t)(b < 0 ? -b : b); - uint32_t q, r; - fp_t res; + uint32_t quotient = 0; + uint32_t bit = 0x10000; - q = u32_divrem(ua, ub, &r); - res = (fp_t)((q << FP_FRAC_BITS) | u32_divrem(r << FP_FRAC_BITS, ub, NULL)); + /* The algorithm requires D >= R */ + while (divider < remainder) + { + divider <<= 1; + bit <<= 1; + } - return sign < 0 ? -res : res; + if (!bit) + return FIX16_OVERFLOW; + + if (divider & 0x80000000) + { + // Perform one step manually to avoid overflows later. + // We know that divider's bottom bit is 0 here. + if (remainder >= divider) + { + quotient |= bit; + remainder -= divider; + } + divider >>= 1; + bit >>= 1; + } + + /* Main division loop */ + while (bit && remainder) + { + if (remainder >= divider) + { + quotient |= bit; + remainder -= divider; + } + + remainder <<= 1; + bit >>= 1; + } + + if (remainder >= divider) + { + quotient++; + } + + fp_t result = quotient; + + /* Figure out the sign of result */ + if ((a ^ b) & 0x80000000) + { + if (result == (fp_t) FIX16_MINIMUM) + return FIX16_OVERFLOW; + + result = -result; + } + + return result; } /* --- CORDIC cosine --- */ diff --git a/libs/bio-lib/src/c/math_test/math_test.rs b/libs/bio-lib/src/c/math_test/math_test.rs index 4726d8c97..8e98472d3 100644 --- a/libs/bio-lib/src/c/math_test/math_test.rs +++ b/libs/bio-lib/src/c/math_test/math_test.rs @@ -1,4 +1,4 @@ -// AUTOGENERATED CODE on 2026-02-20T22:51:59.566972 - do not edit directly! +// AUTOGENERATED CODE on 2026-03-20T22:34:18.600020 - do not edit directly! use bao1x_api::bio_code_aligned;