fn write_sequence(xm: &xmkit::XModule, mut outfile: &File) {
    let used_ptns = get_used_pattern_ids(&xm);

    let new_ptn_indices = used_ptns
        .iter()
        .enumerate()
        .map(|(idx, ptn)| (ptn, idx))
        .collect::<HashMap<&usize, usize>>();

    outfile.write_all("\nsequence\n".as_bytes()).unwrap();

    for it in xm.sequence() {
        outfile
            .write_all(
                format!(
                    "\t!byte ${:x}\n",
                    new_ptn_indices.get(&(it as usize)).unwrap() + 1
                )
                .as_bytes(),
            )
            .unwrap();
    }

    outfile.write_all("\t!byte 0\n".as_bytes()).unwrap();

    outfile
        .write_all("\npattern_lookup_lo\n".as_bytes())
        .unwrap();

    for p in &used_ptns {
        outfile
            .write_all(format!("\t!byte <ptn{:x}\n", p + 1).as_bytes())
            .unwrap()
    }

    outfile.write_all("\t!byte <ptnEnd\n".as_bytes()).unwrap();

    outfile
        .write_all("\npattern_lookup_hi\n".as_bytes())
        .unwrap();

    outfile
        .write_all(format!("\t!byte (ptn{:x}>>4)&$f0\n", used_ptns[0] + 1).as_bytes())
        .unwrap();

    let mut used_ptns_hi = used_ptns;
    const PADDING: usize = 0xff;
    used_ptns_hi.remove(0);
    used_ptns_hi.resize(
        used_ptns_hi.len() + (2 - used_ptns_hi.len() % 2) % 2,
        PADDING,
    );

    for pos in used_ptns_hi.chunks(2) {
        outfile.write_all("\t!byte ".as_bytes()).unwrap();
        if pos[1] == PADDING {
            outfile
                .write_all(format!("(ptn{:x}>>8)&$f\n", pos[0] + 1).as_bytes())
                .unwrap();
        } else {
            outfile
                .write_all(
                    format!(
                        "((ptn{:x}>>8)&$f)|((ptn{:x}>>4)&$f0)\n",
                        pos[0] + 1,
                        pos[1] + 1
                    )
                    .as_bytes(),
                )
                .unwrap();
        }
    }
}
// writes sequence + pattern lookup table
writes sequence + pattern lookup table
writes sequence + pattern lookup table
fn write_sequence(xm: &xmkit::XModule, mut outfile: &File) {
    let used_ptns = get_used_pattern_ids(&xm);
    let new_ptn_indices = used_ptns
        .iter()
        .enumerate()
        .map(|(idx, ptn)| (ptn, idx))
        .collect::<HashMap<&usize, usize>>();
    outfile.write_all("\nsequence\n".as_bytes()).unwrap();
    for it in xm.sequence() {
        outfile
            .write_all(
                format!(
                    "\t!byte ${:x}\n",
                    new_ptn_indices.get(&(it as usize)).unwrap() + 1
                )
                .as_bytes(),
            )
            .unwrap();
    }
    outfile.write_all("\t!byte 0\n".as_bytes()).unwrap();
    outfile
        .write_all("\npattern_lookup_lo\n".as_bytes())
        .unwrap();
    for p in &used_ptns {
        outfile
            .write_all(format!("\t!byte <ptn{:x}\n", p + 1).as_bytes())
            .unwrap()
    }
    outfile.write_all("\t!byte <ptnEnd\n".as_bytes()).unwrap();
    outfile
        .write_all("\npattern_lookup_hi\n".as_bytes())
        .unwrap();
    outfile
        .write_all(format!("\t!byte (ptn{:x}>>4)&$f0\n", used_ptns[0] + 1).as_bytes())
        .unwrap();
    let mut used_ptns_hi = used_ptns;
    const PADDING: usize = 0xff;
    used_ptns_hi.remove(0);
    used_ptns_hi.resize(
        used_ptns_hi.len() + (2 - used_ptns_hi.len() % 2) % 2,
        PADDING,
    );
    for pos in used_ptns_hi.chunks(2) {
        outfile.write_all("\t!byte ".as_bytes()).unwrap();
        if pos[1] == PADDING {
            outfile
                .write_all(format!("(ptn{:x}>>8)&$f\n", pos[0] + 1).as_bytes())
                .unwrap();
        } else {
            outfile
                .write_all(
                    format!(
                        "((ptn{:x}>>8)&$f)|((ptn{:x}>>4)&$f0)\n",
                        pos[0] + 1,
                        pos[1] + 1
                    )
                    .as_bytes(),
                )
                .unwrap();
        }
    }
}
fn write_sequence ( xm : & xmkit :: XModule , mut outfile : & File ) { let used_ptns = get_used_pattern_ids ( & xm ) ; let new_ptn_indices = used_ptns . iter ( ) . enumerate ( ) . map ( | ( idx , ptn ) | ( ptn , idx ) ) . collect :: < HashMap < & usize , usize > > ( ) ; outfile . write_all ( " \n \n " . as_bytes ( ) ) . unwrap ( ) ; for it in xm . sequence ( ) { outfile . write_all ( format ! ( " \t \n " new_ptn_indices get ( ( it as usize ) ) unwrap ( ) 1 ) . as_bytes ( ) , ) . unwrap ( ) ; } outfile . write_all ( " \t \n " . as_bytes ( ) ) . unwrap ( ) ; outfile . write_all ( " \n \n " . as_bytes ( ) ) . unwrap ( ) ; for p in & used_ptns { outfile . write_all ( format ! ( " \t \n " p 1 ) . as_bytes ( ) ) . unwrap ( ) } outfile . write_all ( " \t \n " . as_bytes ( ) ) . unwrap ( ) ; outfile . write_all ( " \n \n " . as_bytes ( ) ) . unwrap ( ) ; outfile . write_all ( format ! ( " \t \n " used_ptns [ 0 ] 1 ) . as_bytes ( ) ) . unwrap ( ) ; let mut used_ptns_hi = used_ptns ; const PADDING : usize = 0xff ; used_ptns_hi . remove ( 0 ) ; used_ptns_hi . resize ( used_ptns_hi . len ( ) + ( 2 - used_ptns_hi . len ( ) % 2 ) % 2 , PADDING , ) ; for pos in used_ptns_hi . chunks ( 2 ) { outfile . write_all ( " \t " . as_bytes ( ) ) . unwrap ( ) ; if pos [ 1 ] == PADDING { outfile . write_all ( format ! ( " \n " pos [ 0 ] 1 ) . as_bytes ( ) ) . unwrap ( ) ; } else { outfile . write_all ( format ! ( " \n " pos [ 0 ] 1 pos [ 1 ] 1 ) . as_bytes ( ) , ) . unwrap ( ) ; } } }
writes sequence + pattern lookup table
writes sequence + pattern lookup table
xm
&xmkit::XModule
outfile
&File
xm
&xmkit::XModule
outfile
&File