Skip to content

Commit

Permalink
add cc65 header files as target output format
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Feb 21, 2020
1 parent a023ec7 commit 1e2d2cd
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 10 deletions.
1 change: 1 addition & 0 deletions samples/sprites/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ project.av: $(RESOURCES)
$(ALOEVERA) -p project.av sprite init terra_sprite terra_imageset
$(ALOEVERA) -p project.av asm ./output/ all
$(ALOEVERA) -p project.av asm -f basic ./output/ all
$(ALOEVERA) -p project.av asm -f cc65 ./output/ all
$(ALOEVERA) -p project.av asm -f bin ./output/ all
# $(ALOEVERA) create sd_image terra.img
# $(ALOEVERA) -p project.av asm -f bin ./output/ -s terra.img all
Expand Down
1 change: 1 addition & 0 deletions src/bin/aloevera.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ subcommands:
short: f
possible_values:
- ca65
- cc65
- basic
- bin
default_value: ca65
Expand Down
6 changes: 5 additions & 1 deletion src/cmd/asm/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,11 @@ where
asm_data.to_string(Some(*line_start + meta_lc))?
);
*line_start += meta_lc + data_lc;
(res, "inc")
let mut ext = "inc";
if *output_format == AsmFormat::Cc65 {
ext = "h"
}
(res, ext)
};
let file_name = match file_name {
Some(f) => format!("{}.meta", f),
Expand Down
35 changes: 32 additions & 3 deletions vera/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ pub enum AsmFormat {
Basic,
/// ca65 assembly
Ca65,
/// cc65-friendly header file
Cc65,
/// Raw Binary file
Bin,
}
Expand All @@ -45,6 +47,7 @@ impl fmt::Display for AsmFormat {
let out = match self {
AsmFormat::Basic => "basic",
AsmFormat::Ca65 => "ca65",
AsmFormat::Cc65 => "cc65",
AsmFormat::Bin => "bin",
};
write!(f, "{}", out)
Expand All @@ -58,6 +61,7 @@ impl FromStr for AsmFormat {
match s.to_lowercase().as_str() {
"ca65" => Ok(AsmFormat::Ca65),
"basic" => Ok(AsmFormat::Basic),
"cc65" => Ok(AsmFormat::Cc65),
"bin" => Ok(AsmFormat::Bin),
other => Err(ErrorKind::UnknownAsmFormat(other.to_owned()).into()),
}
Expand Down Expand Up @@ -97,6 +101,7 @@ impl AssembledString {
for (i, l) in self.assembled_data.iter().enumerate() {
retval += &match self.target_format {
AsmFormat::Ca65 => format!("{}\n", l),
AsmFormat::Cc65 => format!("{}\n", l),
AsmFormat::Basic => format!("{} {}\n", line_start + i, l),
// only metadata should get to this stage
AsmFormat::Bin => format!("{}\n", l),
Expand All @@ -120,12 +125,15 @@ pub struct AssembledPrimitive {
meta: Vec<String>,
/// Assembled raw binary data
data: Vec<u8>,
/// Id, needed by some types of output
id: String,
}

impl AssembledPrimitive {
/// New blank assembly
pub fn new() -> Self {
pub fn new(id: &str) -> Self {
Self {
id: id.to_owned(),
meta: vec![],
data: vec![],
}
Expand Down Expand Up @@ -168,24 +176,40 @@ impl AssembledPrimitive {
/// Output Meta, formatted for assembly target
pub fn assemble_meta(&self, out_format: AsmFormat) -> Result<AssembledString, Error> {
let mut retval = AssembledString::new(&out_format);
if out_format == AsmFormat::Cc65 {
retval.add(format!("/**"));
}
for m in self.meta.iter() {
retval.add(match out_format {
AsmFormat::Ca65 => format!(";{}", m),
AsmFormat::Basic => format!("REM {}", m.to_uppercase()),
AsmFormat::Cc65 => format!(" * {}", m),
AsmFormat::Bin => format!(";{}", m),
});
}
if out_format == AsmFormat::Cc65 {
retval.add(format!(" */"));
}
Ok(retval)
}

/// Output data, formatted for assembly target
pub fn assemble_data(&self, out_format: AsmFormat) -> Result<AssembledString, Error> {
let mut retval = AssembledString::new(&out_format);
if out_format == AsmFormat::Cc65 {
retval.add(format!("#ifndef {}_H", self.id.to_uppercase()));
retval.add(format!("#define {}_H", self.id.to_uppercase()));
retval.add(format!(
"static const unsigned char {}[] = {{",
self.id.to_uppercase()
));
}
for i in (0..self.data.len()).step_by(8) {
let mut curval = String::from("");
curval += &match out_format {
AsmFormat::Ca65 => format!(".byte "),
AsmFormat::Basic => format!("DATA "),
AsmFormat::Cc65 => format!(" "),
AsmFormat::Bin => {
return Err(ErrorKind::InvalidAsmFormat(
"Attempt to format binary data as string".into(),
Expand All @@ -201,6 +225,7 @@ impl AssembledPrimitive {
curval += &match out_format {
AsmFormat::Ca65 => format!("${:02X}", self.data[index]),
AsmFormat::Basic => format!("{}", self.data[index]),
AsmFormat::Cc65 => format!("0x{:02x}", self.data[index]),
AsmFormat::Bin => {
return Err(ErrorKind::InvalidAsmFormat(
"Attempt to format binary data as string".into(),
Expand All @@ -214,13 +239,17 @@ impl AssembledPrimitive {
}
retval.add(curval);
}
if out_format == AsmFormat::Cc65 {
retval.add(format!("}};"));
retval.add(format!("#endif"));
}
Ok(retval)
}
}

#[test]
fn test_assemble() -> Result<(), Error> {
let mut prim = AssembledPrimitive::new();
let mut prim = AssembledPrimitive::new("my_prim");
prim.add_meta("here is some metadata 1".to_owned());
prim.add_meta("here is some metadata 2".to_owned());
prim.add_meta("here is some metadata 4".to_owned());
Expand Down Expand Up @@ -255,7 +284,7 @@ fn test_assemble() -> Result<(), Error> {
println!("{}", data_str);

assert!(data_str.starts_with(".byte $10,"));
assert!(data_str.ends_with(".byte $10,$10\n"));
assert!(data_str.ends_with(".byte $10,$10"));

Ok(())
}
2 changes: 1 addition & 1 deletion vera/src/bitmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl<'a> Assemblable for VeraBitmap<'a> {
}

fn assemble(&self) -> Result<AssembledPrimitive, Error> {
let mut retval = AssembledPrimitive::new();
let mut retval = AssembledPrimitive::new(self.id());
if self.imageset.is_none() {
return Err(ErrorKind::BitmapNoImageSet(format!("{}", self.id)).into());
}
Expand Down
2 changes: 1 addition & 1 deletion vera/src/imageset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -507,7 +507,7 @@ impl Assemblable for VeraImageSet {
if !self.formatted {
return Err(ErrorKind::ImageSetNotFormatted(format!("{}", self.id)).into());
}
let mut retval = AssembledPrimitive::new();
let mut retval = AssembledPrimitive::new(self.id());
retval.add_meta(format!("{} - size is {}", self.id, self.size()));
let depth = match self.depth {
Some(d) => d,
Expand Down
2 changes: 1 addition & 1 deletion vera/src/palette.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl Assemblable for VeraPalette {
}

fn assemble(&self) -> Result<AssembledPrimitive, Error> {
let mut retval = AssembledPrimitive::new();
let mut retval = AssembledPrimitive::new(self.id());
retval.add_meta(format!("{} - size is {}", self.id, self.size()));
for e in self.entries.iter() {
let mut off_0 = e.g << 4;
Expand Down
2 changes: 1 addition & 1 deletion vera/src/sprite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<'a> Assemblable for VeraSprite<'a> {
}

fn assemble(&self) -> Result<AssembledPrimitive, Error> {
let mut retval = AssembledPrimitive::new();
let mut retval = AssembledPrimitive::new(self.id());
if self.imageset.is_none() {
return Err(ErrorKind::SpriteNoImageSet(format!("{}", self.id)).into());
}
Expand Down
4 changes: 2 additions & 2 deletions vera/src/tilemap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ impl Assemblable for VeraTileMapEntry {
}

fn assemble(&self) -> Result<AssembledPrimitive, Error> {
let mut retval = AssembledPrimitive::new();
let mut retval = AssembledPrimitive::new(self.id());
let out_bytes = match self {
VeraTileMapEntry::Text0(index, foreground, background) => {
let mut byte_1: u8 = background << 4;
Expand Down Expand Up @@ -475,7 +475,7 @@ impl Assemblable for VeraTileMap {
if self.tiles.is_empty() {
warn!("tilemap is empty: {}", self.id);
}
let mut retval = AssembledPrimitive::new();
let mut retval = AssembledPrimitive::new(self.id());
// load instructions
let (start_index, stride, skip) = self.calc_start_index_stride_and_skip();
retval.add_meta(format!("{} size is {}", self.id, self.size()));
Expand Down

0 comments on commit 1e2d2cd

Please sign in to comment.