Skip to content

Commit

Permalink
add alternative binary input for vic colour info
Browse files Browse the repository at this point in the history
  • Loading branch information
yeastplume committed Sep 25, 2021
1 parent ae104aa commit 6f36ee2
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 23 deletions.
6 changes: 3 additions & 3 deletions samples/Makefile.vars
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
# Ensure the emulator and rom locations are for your system

ALOEVERA = "../../target/debug/aloevera"
X16_EMU = "../../../x16-emulator/x16emu"
X16_ROM = "../../../x16-rom/build/x16/rom.bin"
X16_EMU_FLAGS = -run -scale 4
X16_EMU = "../../../x16emu_win-r38/x16emu.exe"
X16_ROM = "../../../x16emu_win-r38/rom.bin"
X16_EMU_FLAGS = -run -scale 2
#DEBUG = -debug 9000
5 changes: 3 additions & 2 deletions samples_cbm/petscii_tile/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ NAME = p128

project.av: p128-ref-template.png
$(ALOEVERA) create project project.av
$(ALOEVERA) -p project.av palette import cbm_palette p128-ref-template-short.png
$(ALOEVERA) -p project.av imageset import -k test_set_1 4 8 p128-ref-template-short.png
$(ALOEVERA) -p project.av palette import cbm_palette p128-temp-tileset.png
$(ALOEVERA) -p project.av imageset import -k test_set_1 4 8 p128-temp-tileset.png
$(ALOEVERA) -p project.av imageset format -v test_set_1 cbm_palette 4
$(ALOEVERA) -p project.av asm -f ca65 ./output/ all
$(ALOEVERA) -p project.av asm -f bin ./output/ all

generate_resources: project.av

Expand Down
30 changes: 24 additions & 6 deletions samples_cbm/petscii_tile/p128.s
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
.include "../common/header.inc"

VIC_BMP_START_ADDR = $2000

.code
lda #$00
sta $d020
sta $d021
rts
jmp start

.proc set_mode
lda #$3b
sta $d011
lda #$18
sta $d016
rts
.endproc

.proc load_tile
lda imageset
sta VIC_BMP_START_ADDR
rts
.endproc

start:
jsr set_mode
jsr load_tile
rts

.segment "RODATA"
imageset:
.include "output/palettes/cbm_palette.ca65.inc"
imageset:
.include "output/imagesets/test_set_1.ca65.inc"
47 changes: 42 additions & 5 deletions src/cmd/asm/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,30 @@ where
let conflate = asm_args.format == AsmFormat::Bin || asm_args.conflate_tilemaps;
let asm_meta = code.assemble_meta(asm_args.format.clone(), conflate)?;
let meta_lc = asm_meta.line_count();
let (output, ext) = if asm_args.format == AsmFormat::Bin {
let (output, alt_output, ext, alt_ext) = if asm_args.format == AsmFormat::Bin {
let (file_name, bin_address) = match sel_args.clone() {
Some(s) => (s.out_file.clone(), s.bin_address.clone()),
None => (format!("{}/{}.bin", asm_args.out_dir, v.id()), [0, 0]),
};
common::output_to_file(
&file_name,
&code.data_as_bin(Some(bin_address), true)?,
&code.data_as_bin(Some(bin_address), true, false)?,
&asm_args.sd_image,
)?;
(asm_meta.to_string(None)?, "meta")
if code.has_alt_data() {
let (file_name, bin_address) = match sel_args.clone() {
Some(s) => (s.out_file.clone(), s.bin_address.clone()),
None => (format!("{}/{}.alt.bin", asm_args.out_dir, v.id()), [0, 0]),
};
common::output_to_file(
&file_name,
&code.data_as_bin(Some(bin_address), false, true)?,
&asm_args.sd_image,
)?;
}
(asm_meta.to_string(None)?, None, "meta", "alt.meta")
} else {
let asm_data = code.assemble_data(asm_args.format.clone(), conflate)?;
let asm_data = code.assemble_data(asm_args.format.clone(), conflate, false)?;
let data_lc = asm_data.line_count();
let output = asm_meta.to_string(Some(*line_start))?;
let res = format!(
Expand All @@ -66,10 +77,26 @@ where
);
*line_start += meta_lc + data_lc;
let mut ext = "inc";
let mut alt_ext = "alt.inc";
if asm_args.format == AsmFormat::Cc65 {
ext = "h"
}
(res, ext)
let mut alt_res = None;
if code.has_alt_data() {
let asm_data = code.assemble_data(asm_args.format.clone(), false, true)?;
let data_lc = asm_data.line_count();
let output = "".to_string();
alt_res = Some(format!(
"{}{}",
output,
asm_data.to_string(Some(*line_start + meta_lc))?
));
*line_start += meta_lc + data_lc;
if asm_args.format == AsmFormat::Cc65 {
alt_ext = "alt.h"
}
}
(res, alt_res, ext, alt_ext)
};
let mut file_name = format!(
"{}/{}.{}.{}",
Expand All @@ -95,6 +122,16 @@ where
warn!("Resource {} has a size of {} bytes, which approaches or exceeds the size of Low RAM ({}) bytes. Consider outputting as .BIN instead.", v.id(), size, LOW_RAM_SIZE);
}
assembled_size += v.size_in_bytes(conflate)?;
if let Some(o) = alt_output {
let file_name = format!(
"{}/{}.{}.{}",
asm_args.out_dir,
v.id(),
asm_args.format,
alt_ext
);
common::output_to_file(&file_name, o.as_bytes(), &asm_args.sd_image)?;
}
}
Ok(assembled_size)
}
Expand Down
23 changes: 23 additions & 0 deletions vera/src/asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,8 @@ pub struct AssembledPrimitive {
meta: Vec<String>,
/// Assembled raw binary data
data: Vec<u8>,
/// Alternate raw binary data for a separate file
alt_data: Vec<u8>,
/// Id, needed by some types of output
id: String,
/// Conflation data for tilemaps
Expand All @@ -160,6 +162,7 @@ impl AssembledPrimitive {
id: id.to_owned(),
meta: vec![],
data: vec![],
alt_data: vec![],
conflate_info: None,
}
}
Expand All @@ -182,6 +185,11 @@ impl AssembledPrimitive {
});
}

/// Whether we have alt data
pub fn has_alt_data(&self) -> bool {
return !&self.alt_data.is_empty();
}

/// Add another prim to this one, consuming second
pub fn add_prim(&mut self, mut other: AssembledPrimitive) {
self.meta.append(&mut other.meta);
Expand All @@ -200,13 +208,24 @@ impl AssembledPrimitive {
}
}

/// Add assembled additional byte data
pub fn add_alt_data(&mut self, new_data: &[u8]) {
for b in new_data {
self.alt_data.push(*b);
}
}

/// retrieve bin data formatted as bin data
pub fn data_as_bin(
&self,
address_bytes: Option<[u8; 2]>,
conflate: bool,
use_alt_data: bool,
) -> Result<Vec<u8>, Error> {
let mut data = self.data.clone();
if use_alt_data {
data = self.alt_data.clone();
}
if self.conflate_info.is_some() && conflate {
data = self.conflate_data()?;
}
Expand Down Expand Up @@ -288,8 +307,12 @@ impl AssembledPrimitive {
&self,
out_format: AsmFormat,
conflate: bool,
use_alt_data: bool,
) -> Result<AssembledString, Error> {
let mut data = &self.data;
if use_alt_data {
data = &self.alt_data;
}
let conf_data;
if self.conflate_info.is_some() && conflate {
conf_data = self.conflate_data()?;
Expand Down
43 changes: 36 additions & 7 deletions vera/src/imageset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ use permutate::Permutator;

use std::collections::hash_map::DefaultHasher;
use std::collections::BTreeMap;
use std::collections::HashMap;
use std::fmt;
use std::hash::{Hash, Hasher};

Expand Down Expand Up @@ -599,17 +600,43 @@ impl VeraImageSet {
for list_entry in traversal_list {
let frame = self.frame_at_coord(list_entry.0 as usize, list_entry.1 as usize)?;
// organize palette entries for the frame in the order they appear
let mut used_palette_entries = vec![0];
let mut used_palette_entries: HashMap<u8, u8> = HashMap::new();
//insert black at 0
used_palette_entries.insert(0, 0);
//and white at 3, note we're assuming the color ram will always be set to white
// here, so any cells with a 3rd unique color entry will be ignored in the final
// output for now
used_palette_entries.insert(1, 3); // Palette entry, intended mask
for p in frame.data.iter() {
let index = match p.pal_index {
Some(i) => i,
None => return Err(ErrorKind::PaletteIndexMissing(0, 0, 0).into()),
};
if !used_palette_entries.contains(&index) {
used_palette_entries.push(index);
if !used_palette_entries.contains_key(&index) && used_palette_entries.len() < 4 {
used_palette_entries.insert(index, (used_palette_entries.len() - 1) as u8);
}
}

let mut color_byte = 0u8;
// Output the color byte for this cell, entries 2 and 3 in the palette entires hashmap,
// if they exist

// First entry with value of 1, if exists
let color_1 = used_palette_entries.iter().find(|e| *e.1 == 1);

if let Some(c) = color_1 {
color_byte = *c.0;
color_byte <<= 4;
}
// And value of 2
let color_2 = used_palette_entries.iter().find(|e| *e.1 == 2);

if let Some(c) = color_2 {
color_byte |= *c.0;
}

retval.add_alt_data(&[color_byte]);

for pixel in frame.data.iter() {
let pal_index = match pixel.pal_index {
Some(p) => p,
Expand All @@ -627,10 +654,12 @@ impl VeraImageSet {
}
// transform colour entries into bit pairs
cur_out_byte <<= 2;
let actual_out = used_palette_entries
.iter()
.position(|&r| r == pal_index)
.unwrap() as u8;
let actual_out = match used_palette_entries.get(&pal_index) {
Some(c) => *c,
// This happens when there's more than 2 colours in a cell and color ram
// is fixed to white
None => 2,
};
cur_out_byte |= actual_out;
cur_val_count += 1;
}
Expand Down

0 comments on commit 6f36ee2

Please sign in to comment.