Skip to content

Unmappable memory generator #17

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
120 changes: 120 additions & 0 deletions src/generate/iface/block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
use anyhow::Result;
use proc_macro2::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote;

use crate::generate::{process_array, sorted, split_path, Options};
use crate::ir::*;
use crate::util;

pub fn render(opts: &Options, ir: &IR, b: &Block, path: &str) -> Result<TokenStream> {
let common_path = opts.common_path();

let span = Span::call_site();
let mut items = TokenStream::new();

let addr_ty = if let Some(addr_size) = &b.address_size {
match addr_size {
1..=8 => quote!(u8),
9..=16 => quote!(u16),
17..=32 => quote!(u32),
33..=64 => quote!(u64),
_ => panic!("Invalid addr_size {}", addr_size),
}
} else {
quote!(u32)
};

for i in sorted(&b.items, |i| (i.byte_offset, i.name.clone())) {
let name = Ident::new(&i.name, span);
let offset = i.byte_offset as usize;

let doc = util::doc(&i.description);

match &i.inner {
BlockItemInner::Register(r) => {
let reg_ty = if let Some(fieldset_path) = &r.fieldset {
let _f = ir.fieldsets.get(fieldset_path).unwrap();
util::relative_path(fieldset_path, path)
} else {
match r.bit_size {
1..=8 => quote!(u8),
9..=16 => quote!(u16),
17..=32 => quote!(u32),
33..=64 => quote!(u64),
_ => panic!("Invalid bit_size {}", r.bit_size),
}
};

let access = match r.access {
Access::Read => quote!(#common_path::R),
Access::Write => quote!(#common_path::W),
Access::ReadWrite => quote!(#common_path::RW),
};

let ty = quote!(#common_path::Reg<'_, I, #addr_ty, #reg_ty, #access>);
if let Some(array) = &i.array {
let (len, offs_expr) = process_array(array);
items.extend(quote!(
#doc
#[inline(always)]
pub fn #name(&mut self, n: usize) -> #ty {
assert!(n < #len);
#common_path::Reg::new(self.iface, (#offset + #offs_expr) as #addr_ty)
}
));
} else {
items.extend(quote!(
#doc
#[inline(always)]
pub fn #name(&mut self) -> #ty {
#common_path::Reg::new(self.iface, (#offset) as #addr_ty)
}
));
}
}
BlockItemInner::Block(b) => {
let block_path = &b.block;
let _b2 = ir.blocks.get(block_path).unwrap();
let ty = util::relative_path(block_path, path);
if let Some(array) = &i.array {
let (len, offs_expr) = process_array(array);

items.extend(quote!(
#doc
#[inline(always)]
pub const fn #name(self, n: usize) -> #ty {
assert!(n < #len);
unsafe { #ty::from_ptr(self.ptr.add(#offset + #offs_expr) as _) }
}
));
} else {
items.extend(quote!(
#doc
#[inline(always)]
pub const fn #name(self) -> #ty {
unsafe { #ty::from_ptr(self.ptr.add(#offset) as _) }
}
));
}
}
}
}

let (_, name) = split_path(path);
let name = Ident::new(name, span);
let doc = util::doc(&b.description);
let out = quote! {
#doc
pub struct #name<'a, I> {
pub iface: &'a mut I,
pub addr: usize,
}

impl<'a, I> #name<'a, I> {
#items
}
};

Ok(out)
}
90 changes: 90 additions & 0 deletions src/generate/iface/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use core::marker::PhantomData;

#[derive(Copy, Clone, PartialEq, Eq)]
pub struct RW;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct R;
#[derive(Copy, Clone, PartialEq, Eq)]
pub struct W;

/// Interface to read and write register values
pub trait Interface<K, V> {
type Error: core::fmt::Debug;
fn read(&mut self, addr: K) -> Result<V, Self::Error>;
fn write(&mut self, addr: K, value: V) -> Result<(), Self::Error>;
}

mod sealed {
use super::*;
pub trait Access {}
impl Access for R {}
impl Access for W {}
impl Access for RW {}
}

pub trait Access: sealed::Access + Copy {}
impl Access for R {}
impl Access for W {}
impl Access for RW {}

pub trait Read: Access {}
impl Read for RW {}
impl Read for R {}

pub trait Write: Access {}
impl Write for RW {}
impl Write for W {}

#[derive(PartialEq, Eq)]
pub struct Reg<'a, I, K, V, A> {
iface: &'a mut I,
addr: K,
phantom: PhantomData<(V, A)>,
}

impl<'a, I, K: Copy, V, A: Access> Reg<'a, I, K, V, A> {
#[inline(always)]
pub fn new(iface: &'a mut I, addr: K) -> Self {
Self {
iface,
addr,
phantom: PhantomData,
}
}
}

impl<'a, I: Interface<K, V>, K: Copy, V, A: Read> Reg<'a, I, K, V, A> {
#[inline(always)]
pub fn read(&mut self) -> Result<V, I::Error> {
self.iface.read(self.addr)
}
}

impl<'a, I: Interface<K, V>, K: Copy, V, A: Write> Reg<'a, I, K, V, A> {
#[inline(always)]
pub fn write_value(&mut self, val: V) -> Result<(), I::Error> {
self.iface.write(self.addr, val)
}
}

impl<'a, I: Interface<K, V>, K: Copy, V: Copy + core::default::Default, A: Write>
Reg<'a, I, K, V, A>
{
#[inline(always)]
pub fn write<R>(&mut self, f: impl FnOnce(&mut V) -> R) -> Result<R, I::Error> {
let mut val = Default::default();
let res = f(&mut val);
self.write_value(val)?;
Ok(res)
}
}

impl<'a, I: Interface<K, V>, K: Copy, V, A: Read + Write> Reg<'a, I, K, V, A> {
#[inline(always)]
pub fn modify<R>(&mut self, f: impl FnOnce(&mut V) -> R) -> Result<R, I::Error> {
let mut val = self.read()?;
let res = f(&mut val);
self.write_value(val)?;
Ok(res)
}
}
3 changes: 1 addition & 2 deletions src/generate/device.rs → src/generate/iface/device.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use quote::quote;

use crate::ir::*;
use crate::util::{self, ToSanitizedUpperCase};

use super::sorted;
use crate::generate::sorted;

pub fn render(_opts: &super::Options, ir: &IR, d: &Device, path: &str) -> Result<TokenStream> {
let mut out = TokenStream::new();
Expand Down
4 changes: 2 additions & 2 deletions src/generate/enumm.rs → src/generate/iface/enumm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ use quote::quote;

use crate::ir::*;
use crate::util;

use super::sorted;
use crate::generate::sorted;

pub fn render(_opts: &super::Options, _ir: &IR, e: &Enum, path: &str) -> Result<TokenStream> {
let span = Span::call_site();
Expand Down Expand Up @@ -123,5 +122,6 @@ pub fn render(_opts: &super::Options, _ir: &IR, e: &Enum, path: &str) -> Result<
}
});


Ok(out)
}
137 changes: 137 additions & 0 deletions src/generate/iface/fieldset.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
use anyhow::Result;
use proc_macro2::TokenStream;
use proc_macro2::{Ident, Span};
use quote::quote;

use crate::ir::*;
use crate::util;
use crate::generate::{Options, sorted, split_path, process_array};

pub fn render(_opts: &Options, ir: &IR, fs: &FieldSet, path: &str) -> Result<TokenStream> {
let span = Span::call_site();
let mut items = TokenStream::new();

let ty = match fs.bit_size {
1..=8 => quote!(u8),
9..=16 => quote!(u16),
17..=32 => quote!(u32),
33..=64 => quote!(u64),
_ => panic!("Invalid bit_size {}", fs.bit_size),
};

for f in sorted(&fs.fields, |f| (f.bit_offset, f.name.clone())) {
let name = Ident::new(&f.name, span);
let name_set = Ident::new(&format!("set_{}", f.name), span);
let bit_offset = f.bit_offset as usize;
let _bit_size = f.bit_size as usize;
let mask = util::hex(1u64.wrapping_shl(f.bit_size).wrapping_sub(1));
let doc = util::doc(&f.description);
let field_ty: TokenStream;
let to_bits: TokenStream;
let from_bits: TokenStream;

if let Some(e_path) = &f.enumm {
let Some(e) = ir.enums.get(e_path) else {
panic!("missing enum {}", e_path);
};

let enum_ty = match e.bit_size {
1..=8 => quote!(u8),
9..=16 => quote!(u16),
17..=32 => quote!(u32),
33..=64 => quote!(u64),
_ => panic!("Invalid bit_size {}", e.bit_size),
};

field_ty = util::relative_path(e_path, path);
to_bits = quote!(val.to_bits() as #ty);
from_bits = quote!(#field_ty::from_bits(val as #enum_ty));
} else {
field_ty = match f.bit_size {
1 => quote!(bool),
2..=8 => quote!(u8),
9..=16 => quote!(u16),
17..=32 => quote!(u32),
33..=64 => quote!(u64),
_ => panic!("Invalid bit_size {}", f.bit_size),
};
to_bits = quote!(val as #ty);
from_bits = if f.bit_size == 1 {
quote!(val != 0)
} else {
quote!(val as #field_ty)
}
}

if let Some(array) = &f.array {
let (len, offs_expr) = process_array(array);
items.extend(quote!(
#doc
#[inline(always)]
pub const fn #name(&self, n: usize) -> #field_ty{
assert!(n < #len);
let offs = #bit_offset + #offs_expr;
let val = (self.0 >> offs) & #mask;
#from_bits
}
#doc
#[inline(always)]
pub fn #name_set(&mut self, n: usize, val: #field_ty) {
assert!(n < #len);
let offs = #bit_offset + #offs_expr;
self.0 = (self.0 & !(#mask << offs)) | (((#to_bits) & #mask) << offs);
}
));
} else {
items.extend(quote!(
#doc
#[inline(always)]
pub const fn #name(&self) -> #field_ty{
let val = (self.0 >> #bit_offset) & #mask;
#from_bits
}
#doc
#[inline(always)]
pub fn #name_set(&mut self, val: #field_ty) {
self.0 = (self.0 & !(#mask << #bit_offset)) | (((#to_bits) & #mask) << #bit_offset);
}
));
}
}

let (_, name) = split_path(path);
let name = Ident::new(name, span);
let doc = util::doc(&fs.description);

let out = quote! {
#doc
#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq)]
pub struct #name (pub #ty);

impl #name {
#items
}

impl Default for #name {
#[inline(always)]
fn default() -> #name {
#name(0)
}
}

impl From<#ty> for #name {
fn from(val: #ty) -> #name {
#name(val)
}
}

impl From<#name> for #ty {
fn from(val: #name) -> #ty {
val.0
}
}
};

Ok(out)
}
Loading