mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-16 08:38:26 +00:00
add optimized codegen path
This commit is contained in:
parent
1dec7e6e7d
commit
7da36a15b0
1 changed files with 173 additions and 0 deletions
|
|
@ -354,6 +354,27 @@ def main():
|
|||
else:
|
||||
patchlen += 1
|
||||
|
||||
# test to see if we can emit an optimized version of the code
|
||||
is_wellformed = True
|
||||
for frame_rec in patchdata_sorted:
|
||||
frame = frame_rec[1]
|
||||
words_used = [0] * 101
|
||||
for word in range(101):
|
||||
if frame[word] == None:
|
||||
continue
|
||||
words_used[word] = 1
|
||||
if words_used[:32] != [1] * 32:
|
||||
is_wellformed = False
|
||||
if words_used[32:] != [0] * (101-32):
|
||||
is_wellformed = False
|
||||
|
||||
if is_wellformed:
|
||||
print("Patch list is well formed, generating optimized code")
|
||||
generate_optimized(f, patchdata_sorted, patchlen)
|
||||
exit(0)
|
||||
else:
|
||||
print("Patch list is irregular, generating general-case code (is_wellformed: {})".format(is_wellformed))
|
||||
|
||||
f.write("""
|
||||
//! this file was auto-generated by key2bits.py on {}
|
||||
//! manual regeneration is needed for the following cases:
|
||||
|
|
@ -472,7 +493,9 @@ pub fn should_patch(frame: u32) -> bool {{
|
|||
}}
|
||||
}}
|
||||
""".format(min(frame_nos), max(frame_nos)))
|
||||
generate_test(f, patchdata_sorted)
|
||||
|
||||
def generate_test(f, patchdata_sorted):
|
||||
f.write("""
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
|
|
@ -510,5 +533,155 @@ mod tests {
|
|||
}
|
||||
""")
|
||||
|
||||
def generate_optimized(f, patchdata_sorted, patchlen):
|
||||
sequences = []
|
||||
sequence = []
|
||||
prev_frame = None
|
||||
for frame_rec in patchdata_sorted:
|
||||
if prev_frame == None:
|
||||
prev_frame = frame_rec[0]
|
||||
sequence.append(frame_rec)
|
||||
else:
|
||||
if frame_rec[0] - prev_frame != 1:
|
||||
sequences.append(sequence)
|
||||
sequence = [frame_rec]
|
||||
else:
|
||||
sequence.append(frame_rec)
|
||||
prev_frame = frame_rec[0]
|
||||
sequences.append(sequence)
|
||||
|
||||
# import pprint
|
||||
# pprint.pprint(sequences, indent=6)
|
||||
|
||||
f.write("""
|
||||
//! this file was auto-generated by key2bits.py on {}
|
||||
//! manual regeneration is needed for the following cases:
|
||||
//! - rom.db changes (that is, the KEYROM pcells have been moved to a new location)
|
||||
//! - entirely different FPGA target (in which case, the db/ files also need to be regenerated
|
||||
//! (this is a long process if you're not targting a prjxray pre-build target))
|
||||
//! - bug fixes and enhancements to these routines
|
||||
""".format(str(datetime.now())))
|
||||
|
||||
f.write("""
|
||||
pub const PATCH_FRAMES: [u32; {}] = [\n""".format(len(patchdata_sorted)))
|
||||
for frame_rec in patchdata_sorted:
|
||||
f.write(" 0x{:x},\n".format(frame_rec[0]))
|
||||
f.write("];\n")
|
||||
|
||||
for sequence in sequences:
|
||||
f.write("""
|
||||
const PATCH_TABLE_{}: [u8; {}] = [\n""".format(sequence[0][0], len(sequence) * 2 * 32 * 32))
|
||||
for frame_rec in sequence:
|
||||
f.write(" // frame 0x{:x} ({})".format(frame_rec[0], frame_rec[0]))
|
||||
for bitdict in frame_rec[1]:
|
||||
if bitdict == None:
|
||||
continue
|
||||
for index in range(32):
|
||||
if (index % 16 == 0):
|
||||
f.write("\n ")
|
||||
f.write("{:03},{:03},".format(bitdict[index][0], bitdict[index][1]))
|
||||
f.write("\n")
|
||||
f.write("];\n")
|
||||
|
||||
f.write("""
|
||||
/// patch a frame at a given relative positition and offset in the framestream
|
||||
/// to insert a key ROM.
|
||||
///
|
||||
/// frame is the frame number in the framestream
|
||||
/// offset is the offset in the frame, from 0-100 (101 words)
|
||||
/// note that each primitive in a bitstream is a u32
|
||||
///
|
||||
/// This code path has been optimized because the patch set is well-formed, such that
|
||||
/// the patches are all located in frames 0-31, and are mostly sequential in frames.
|
||||
/// This allows us to use direct array accessors with index operations, instead of a
|
||||
/// more general data structure. This method is less readable, but saves a significant
|
||||
/// amount of code space and reduces compile times. Refer to the generation script
|
||||
/// if a more general form is desired; this option is retained for reference purposes.
|
||||
///
|
||||
/// returns None if the position should not be patched
|
||||
/// returns a tuple of the value and its inverse; this is done to reduce the timing
|
||||
/// sidechannel. The inverse value needs to be consumed to prevent the compiler
|
||||
/// from optimizing out that path.
|
||||
""")
|
||||
|
||||
f.write("""
|
||||
/// a fast check to see if a given frame is within the range of frames that are
|
||||
/// patchable. Use this to wrap calls to `patch_frame` as a performance optimization.
|
||||
pub fn should_patch(frame: u32) -> bool {
|
||||
""")
|
||||
f.write(" if")
|
||||
first_element = True
|
||||
for sequence in sequences:
|
||||
if first_element:
|
||||
f.write(" (frame >= 0x{:x} && frame <= 0x{:x})\n".format(sequence[0][0], sequence[-1][0]))
|
||||
first_element = False
|
||||
else:
|
||||
f.write(" || (frame >= 0x{:x} && frame <= 0x{:x})\n".format(sequence[0][0], sequence[-1][0]))
|
||||
f.write(""" {
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}""")
|
||||
|
||||
f.write("""
|
||||
fn get_patch_subtable(frame: u32, offset: u32, table_base: u32) -> &'static [u8] {
|
||||
// ASSUME: all offsets are checked before here. Worst case, we throw a panic because of an OOB index.
|
||||
// A frame table consists of:
|
||||
// - 32 patchable words in a frame (it is a convenient coincidence that the
|
||||
// patchable words are at positions 0-31: if the ROM LUTs were implemented
|
||||
// differently, this may not be the case)
|
||||
// - Each word to patch is 32 bits long, thus requiring as many entries
|
||||
// - Each entry consisting of 2 items:
|
||||
// - A u8 address in the conventionally-ordered source key ROM
|
||||
// - A u8 bit position of the data at that address in the same key ROM
|
||||
// Thus a patch table says for a given frame and offset, you may replace the
|
||||
// values with those found at the given address and bit offsets in a key ROM
|
||||
// to program that key ROM into the LUT
|
||||
let base = ((frame - table_base) * 2 * 32 * 32 + (offset * 2 * 32)) as usize;
|
||||
match table_base {
|
||||
""")
|
||||
for sequence in sequences:
|
||||
f.write(" {} => &PATCH_TABLE_{}[base..base + 2 * 32],\n".format(sequence[0][0], sequence[0][0]))
|
||||
f.write(""" _ => panic!("invalid table base in patch.rs")
|
||||
}
|
||||
}
|
||||
""")
|
||||
|
||||
f.write("""
|
||||
pub fn patch_frame(frame: u32, offset: u32, rom: &[u32]) -> Option<(u32, u32)> {
|
||||
if offset >= 32 {
|
||||
return None;
|
||||
}
|
||||
if should_patch(frame) {
|
||||
// ASSUME: `frame` meets the requirements outlined above
|
||||
let table_base = match frame {
|
||||
""")
|
||||
for sequence in sequences:
|
||||
base = sequence[0][0]
|
||||
for run in sequence:
|
||||
f.write(" {} => {},\n".format(run[0], base))
|
||||
f.write(""" _ => panic!("invalid frame in patch.rs"),
|
||||
};
|
||||
let subtable = get_patch_subtable(frame, offset, table_base);
|
||||
let mut data: u32 = 0;
|
||||
let mut data_inv: u32 = 0;
|
||||
for bit in 0..32 {
|
||||
let (adr, patch_bit) = (subtable[bit * 2], subtable[bit * 2 + 1]);
|
||||
let romval: u32 = rom[adr as usize] & (1 << patch_bit);
|
||||
if romval != 0 {
|
||||
data |= 1 << bit;
|
||||
} else {
|
||||
data_inv |= 1 << bit;
|
||||
}
|
||||
}
|
||||
Some((data, data_inv))
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
""")
|
||||
generate_test(f, patchdata_sorted)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
|
|
|
|||
Loading…
Reference in a new issue