def test_array_of_conditionals(): test_struct = [ ("cond", b.uint8), ("foos", b.array(3, (b.CONDITIONAL, "cond", { 1: [("foo", b.nibble), b.padding(4)], 2: [("bar", b.bit), b.padding(7)], 4: [("baz", b.semi_nibble), b.padding(6)] }))) ] test_data = bytearray([1, 0b10000101, 0b01010110, 0b11010101]) test_parsed = b.parse(test_data, test_struct) assert_equal(test_parsed.cond, 1) assert_equal(test_parsed.foos[0].foo, 0b1000) assert_equal(test_parsed.foos[1].foo, 0b0101) assert_equal(test_parsed.foos[2].foo, 0b1101) test_parsed.cond = 4 assert_equal(test_parsed.cond, 4) assert_equal(test_parsed.foos[0].baz, 0b10) assert_equal(test_parsed.foos[1].baz, 0b01) assert_equal(test_parsed.foos[2].baz, 0b11)
from .vendor.six.moves import range def padded_hex(pad_count): return lambda x: ("0x%%0%dx" % (pad_count)) % (x) EMPTY_BLOCK = 0xff # File management structure (starts at 0x8000) compressed_sav_file = [ # Up to 32 files (songs) can be saved to one cartridge ("filenames", b.array(32, b.string(8))), # Each file has a monotonically increasing version number ("file_versions", b.array(32, b.byte)), b.padding(30 * 8), ("sram_init_check", b.string(2)), # set to 'jk' on init # The file that is currently active ("active_file", b.byte), # Table mapping blocks to files. ("block_alloc_table", b.array(191, b.byte)) ] # Should be 0x4000 bytes long sample_kit = [ ("magic_number", b.string(2)), # should be 0x60, 0x40 # The address of the first byte after each sample, or 0 if the sample is # unused ("sample_end_addresses", b.array(15, b.string(2))), b.padding(2 * 8), # For samples with names less than three characters long, sample names are
import bread as b import sys def hex_array(x): return str(map(hex, x)) nsf_header = [('magic_number', b.array(5, b.byte), { "str_format": hex_array }), ('version', b.byte), ('total_songs', b.byte), ('starting_song', b.byte), ('load_addr', b.uint16, { "str_format": hex }), ('init_addr', b.uint16, { "str_format": hex }), ('play_addr', b.uint16, { "str_format": hex }), ('title', b.string(32)), ('artist', b.string(32)), ('copyright', b.string(32)), ('ntsc_speed', b.uint16), ('bankswitch_init', b.array(8, b.byte), { "str_format": hex_array }), ('pal_speed', b.uint16), ('ntsc', b.boolean), ('pal', b.boolean), ('ntsc_and_pal', b.boolean), (b.padding(6)), ('vrc6', b.boolean), ('vrc7', b.boolean), ('fds', b.boolean), ('mmc5', b.boolean), ('namco_106', b.boolean), ('fme07', b.boolean), (b.padding(2)), (b.padding(32))] with open(sys.argv[1], 'r') as fp: header = b.parse(fp, nsf_header) print header
import bread as b import bitstring # Shared structs for bread struct test test_struct = [ { "endianness": b.BIG_ENDIAN }, ("flag_one", b.boolean), ("flag_two", b.boolean), ("flag_three", b.boolean), ("flag_four", b.boolean), ("first", b.uint8), (b.padding(2),), b.padding(2), ("blah", b.uint16), ("second", b.int64), ("third", b.uint64), ("fourth", b.int8) ] test_array_struct = [ { "endianness": b.BIG_ENDIAN }, ("first", b.uint8), ("flags", b.array(8, b.boolean)), ("last", b.uint8)]
[ ('magic_number', b.array(5, b.byte), {"str_format": hex_array}) , ('version', b.byte) , ('total_songs', b.byte) , ('starting_song', b.byte) , ('load_addr', b.uint16, {"str_format": hex}) , ('init_addr', b.uint16, {"str_format": hex}) , ('play_addr', b.uint16, {"str_format": hex}) , ('title', b.string(32)) , ('artist', b.string(32)) , ('copyright', b.string(32)) , ('ntsc_speed', b.uint16) , ('bankswitch_init', b.array(8, b.byte), {"str_format": hex_array}) , ('pal_speed', b.uint16) , ('tv_std', b.boolean, {"str_format": lambda x: "PAL" if x else "NTSC"}) , ('ntsc_and_pal', b.boolean) , (b.padding(6)) , ('vrc6', b.boolean) , ('vrc7', b.boolean) , ('fds', b.boolean) , ('mmc5', b.boolean) , ('namco_106', b.boolean) , ('fme07', b.boolean) , (b.padding(2)) , (b.padding(32)) ] class Instr: def __init__(self, op, amode, opand, size): self.op = op self.amode = amode
{"str_format": hex_array}), ('version', b.byte), ('total_songs', b.byte), ('starting_song', b.byte), ('load_addr', b.uint16, {"str_format": hex}), ('init_addr', b.uint16, {"str_format": hex}), ('play_addr', b.uint16, {"str_format": hex}), ('title', b.string(32)), ('artist', b.string(32)), ('copyright', b.string(32)), ('ntsc_speed', b.uint16), ('bankswitch_init', b.array(8, b.byte), {"str_format": hex_array}), ('pal_speed', b.uint16), ('ntsc', b.boolean), ('pal', b.boolean), ('ntsc_and_pal', b.boolean), (b.padding(6)), ('vrc6', b.boolean), ('vrc7', b.boolean), ('fds', b.boolean), ('mmc5', b.boolean), ('namco_106', b.boolean), ('fme07', b.boolean), (b.padding(2)), (b.padding(32)) ] with open(sys.argv[1], 'r') as fp: header = b.parse(fp, nsf_header) print header
'offset': 1 }), ('feedback', b.uint8), ('oscillator_sync', b.uint8), ('lfo_speed', b.uint8), ('lfo_delay', b.uint8), ('lfo_pitch_mod_depth', b.uint8), ('lfo_amp_mod_depth', b.uint8), ('lfo_sync', b.uint8), ('lfo_waveform', b.enum(8, enums['waveforms'])), ('pitch_mod_sensitivity', b.uint8), ('transpose', b.uint8), ('name', b.string(10, NAME_ENCODING))] compressed_operator = [ ('eg_rates', b.array(4, b.uint8)), ('eg_levels', b.array(4, b.uint8)), ('keyboard_level_scaling_break_point', b.uint8), # C3 = 0x27 ('keyboard_level_scaling_left_depth', b.uint8), ('keyboard_level_scaling_right_depth', b.uint8), b.padding(4), # Byte 11 ('keyboard_level_scaling_right_curve', b.enum(2, enums['curves'])), ('keyboard_level_scaling_left_curve', b.enum(2, enums['curves'])), # Byte 12 b.padding(1), ('osc_detune', b.intX(4), { 'offset': -7 }), ('keyboard_rate_scaling', b.intX(3)), # Byte 13 b.padding(3), ('key_velocity_sensitivity', b.intX(3)), ('amp_mod_sensitivity', b.intX(2)), # Byte 14 ('output_level', b.uint8),
import struct, sys, pprint, unittest, itertools, tempfile, os, json from nose.tools import assert_equal, assert_not_equal, assert_true, \ assert_false, assert_raises, raises import bread as b import bitstring # Shared structs for bread struct test test_struct = [{ "endianness": b.BIG_ENDIAN }, ("flag_one", b.boolean), ("flag_two", b.boolean), ("flag_three", b.boolean), ("flag_four", b.boolean), ("first", b.uint8), (b.padding(2), ), b.padding(2), ("blah", b.uint16), ("second", b.int64), ("third", b.uint64), ("fourth", b.int8)] test_array_struct = [{ "endianness": b.BIG_ENDIAN }, ("first", b.uint8), ("flags", b.array(8, b.boolean)), ("last", b.uint8)] nested_array_struct = [{ "endianness": b.BIG_ENDIAN }, ("first", b.uint8), ("matrix", b.array(3, b.array(3, b.uint8))), ("last", b.uint8)] simple_struct = [("length", b.uint8), ("ok", b.boolean)] offset_struct = [("length", b.uint8, {"offset": 1})]
import bread as b from .vendor.six.moves import range def padded_hex(pad_count): return lambda x: ("0x%%0%dx" % (pad_count)) % (x) EMPTY_BLOCK = 0xff # File management structure (starts at 0x8000) compressed_sav_file = [ # Up to 32 files (songs) can be saved to one cartridge ("filenames", b.array(32, b.string(8))), # Each file has a monotonically increasing version number ("file_versions", b.array(32, b.byte)), b.padding(30 * 8), ("sram_init_check", b.string(2)), # set to 'jk' on init # The file that is currently active ("active_file", b.byte), # Table mapping blocks to files. ("block_alloc_table", b.array(191, b.byte)) ] # Should be 0x4000 bytes long sample_kit = [ ("magic_number", b.string(2)), # should be 0x60, 0x40 # The address of the first byte after each sample, or 0 if the sample is # unused ("sample_end_addresses", b.array(15, b.string(2))), b.padding(2 * 8), # For samples with names less than three characters long, sample names are