mod image;

use proc_macro::TokenStream;
use quote::{format_ident, quote};

#[proc_macro]
pub fn define_regs(_input: TokenStream) -> TokenStream {
    let mut tokens = proc_macro2::TokenStream::new();

    for n in 0..32usize {
        let load = format!("li x{}, {{}}", n);
        let store = format!("mv {{}}, x{}", n);
        let ident = format_ident!("X{}", n);
        let t = quote! {
            impl Reg<#n> {
                pub fn write_const<const X: u64>() {
                    unsafe {
                        core::arch::asm!(#load, const X)
                    }
                }
                pub fn read() -> u64 {
                    let out: u64;
                    unsafe {
                        core::arch::asm!(#store, out(reg) out)
                    }
                    out
                }
            }
            pub type #ident = Reg<#n>;
        };
        tokens.extend(t);
    }

    tokens.into()
}

#[proc_macro]
pub fn include_font_plate(input: TokenStream) -> TokenStream {
    image::include_font_plate_impl(input)
}
