Ejemplo n.º 1
0
def test_str():
    str_test = [("msg", b.string(5))]

    data = bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f])
    result = b.parse(data, str_test)
    assert_equal(result.msg.decode('utf-8'), "hello")

    assert_equal(b.write(result, str_test), data)
Ejemplo n.º 2
0
def test_str():
    str_test = [("msg", b.string(5))]

    data = bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f])
    result = b.parse(data, str_test)
    assert_equal(result.msg.decode('utf-8'), "hello")

    assert_equal(b.write(result, str_test), data)
Ejemplo n.º 3
0
def test_parse_str():
    test_struct = [("str", b.string(13))]

    test_str = str("gabbagabbahey")

    test_parsed = b.parse(test_str, test_struct)

    assert_equal(test_parsed.str.decode('utf-8'), "gabbagabbahey")
Ejemplo n.º 4
0
def test_str():
    str_test = [("msg", b.string(5))]

    data = bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f])
    result = b.parse(data, str_test)

    assert result.msg == "hello"

    assert b.write(result, str_test) == data
Ejemplo n.º 5
0
def test_parse_str():
    test_struct = [
        ("str", b.string(13))
    ]

    test_str = str("gabbagabbahey")

    test_parsed = b.parse(test_str, test_struct)

    assert_equal(test_parsed.str.decode('utf-8'), "gabbagabbahey")
Ejemplo n.º 6
0
def test_parse_str():
    test_struct = [
        ("str", b.string(13))
    ]

    test_str = str("gabbagabbahey")

    test_parsed = b.parse(test_str, test_struct)

    assert test_parsed.str == "gabbagabbahey"
Ejemplo n.º 7
0
def test_str():
    str_test = [("msg", b.string(5))]

    data = bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f])
    result = b.parse(data, str_test)
    assert result.msg == "hello"

    assert b.write(result, str_test) == data

    assert result.as_json() == json.dumps({'msg': 'hello'})
Ejemplo n.º 8
0
def test_read_and_write_prefix():
    lsdsng_preamble = [("name", b.string(8)), ("version", b.byte)]

    data = 'hellomon'.encode('utf-8')

    data += bytearray([10, 20, 30, 40, 50])

    parsed = b.parse(data, lsdsng_preamble)

    assert_equal(len(parsed), 9 * 8)

    output_bytes = b.write(parsed)

    assert_equal(len(output_bytes), 9)
Ejemplo n.º 9
0
def test_get_slice():
    data = bytearray([0x61, 0x62, 0x63, 0x64, 0x65, 0x66])

    slice_test_format = [('arr', b.array(6, b.string(1)))]

    slice_test = b.parse(data, slice_test_format)

    assert_equal([b'a', b'b', b'c', b'd', b'e', b'f'], list(slice_test.arr))

    assert_equal([b'c', b'd', b'e', b'f'], slice_test.arr[2:])
    assert_equal([b'a', b'b'], slice_test.arr[:2])
    assert_equal([b'f', b'e', b'd', b'c', b'b', b'a'], slice_test.arr[::-1])
    assert_equal([b'c', b'd', b'e'], slice_test.arr[2:5])
    assert_equal([b'f', b'e', b'd'], slice_test.arr[5:2:-1])
    assert_equal([b'f', b'e', b'd'], slice_test.arr[:2:-1])
Ejemplo n.º 10
0
def test_new():
    format_spec = [("greeting", b.string(5)), ("age", b.nibble)]

    empty_struct = b.new(format_spec)

    assert_equal(len(empty_struct), 8 * 5 + 4)

    assert_equal(empty_struct.greeting, b'\x00\x00\x00\x00\x00')
    assert_equal(empty_struct.age, 0)

    empty_struct.greeting = 'hello'
    empty_struct.age = 0xb

    output_bytes = b.write(empty_struct)
    assert_equal(output_bytes, bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xb0]))
Ejemplo n.º 11
0
def test_get_slice():
    data = bytearray([0x61, 0x62, 0x63, 0x64, 0x65, 0x66])

    slice_test_format = [('arr', b.array(6, b.string(1)))]

    slice_test = b.parse(data, slice_test_format)

    assert_equal([b'a', b'b', b'c', b'd', b'e', b'f'], list(slice_test.arr))

    assert_equal([b'c', b'd', b'e', b'f'], slice_test.arr[2:])
    assert_equal([b'a', b'b'], slice_test.arr[:2])
    assert_equal([b'f', b'e', b'd', b'c', b'b', b'a'], slice_test.arr[::-1])
    assert_equal([b'c', b'd', b'e'], slice_test.arr[2:5])
    assert_equal([b'f', b'e', b'd'], slice_test.arr[5:2:-1])
    assert_equal([b'f', b'e', b'd'], slice_test.arr[:2:-1])
Ejemplo n.º 12
0
def _test_new(data=None):
    format_spec = [("greeting", b.string(5)),
                   ("age", b.nibble)]

    empty_struct = b.new(format_spec, data=data)

    assert len(empty_struct) == 8 * 5 + 4

    assert empty_struct.greeting == b'\x00\x00\x00\x00\x00'.decode('utf-8')
    assert empty_struct.age == 0

    empty_struct.greeting = 'hello'
    empty_struct.age = 0xb

    output_bytes = b.write(empty_struct)
    assert output_bytes == bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xb0])
Ejemplo n.º 13
0
def test_str_unicode():
    str_test = [("msg", b.string(5))]

    data = bytearray([104, 101, 108, 108, 111])
    result = b.parse(data, str_test)

    assert_equal(result.msg.decode('utf-8'), "hello")
    assert_equal(b.write(result, str_test), data)

    result.msg = "abate"

    output_data = b.write(result, str_test)

    edited_result = b.parse(output_data, str_test)

    assert_equal(result.msg, "abate")
Ejemplo n.º 14
0
def test_str_unicode():
    str_test = [("msg", b.string(5))]

    data = bytearray([104, 101, 108, 108, 111])
    result = b.parse(data, str_test)

    assert result.msg == "hello"
    assert b.write(result, str_test) == data

    result.msg = "abate"

    output_data = b.write(result, str_test)

    edited_result = b.parse(output_data, str_test)

    assert result.msg == "abate"
Ejemplo n.º 15
0
def test_str_unicode():
    str_test = [("msg", b.string(5))]

    data = bytearray([104, 101, 108, 108, 111])
    result = b.parse(data, str_test)

    assert_equal(result.msg.decode('utf-8'), "hello")
    assert_equal(b.write(result, str_test), data)

    result.msg = "abate"

    output_data = b.write(result, str_test)

    edited_result = b.parse(output_data, str_test)

    assert_equal(result.msg, "abate")
Ejemplo n.º 16
0
def test_new():
    format_spec = [("greeting", b.string(5)),
                   ("age", b.nibble)]

    empty_struct = b.new(format_spec)

    assert_equal(len(empty_struct), 8 * 5 + 4)

    assert_equal(empty_struct.greeting, b'\x00\x00\x00\x00\x00')
    assert_equal(empty_struct.age, 0)

    empty_struct.greeting = 'hello'
    empty_struct.age = 0xb

    output_bytes = b.write(empty_struct)
    assert_equal(output_bytes, bytearray([0x68, 0x65, 0x6c, 0x6c, 0x6f, 0xb0]))
Ejemplo n.º 17
0
def test_read_and_write_prefix():
    lsdsng_preamble = [
        ("name", b.string(8)),
        ("version", b.byte)
    ]

    data = 'hellomon'.encode('utf-8')

    data += bytearray([10, 20, 30, 40, 50])

    parsed = b.parse(data, lsdsng_preamble)

    assert_equal(len(parsed), 9 * 8)

    output_bytes = b.write(parsed)

    assert_equal(len(output_bytes), 9)
Ejemplo n.º 18
0
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),
Ejemplo n.º 19
0
        'offset': -7
    })  # Range from -7 to 7
]

raw_voice = [('operators', b.array(6, raw_operator)),
             ('pitch_eg_rates', b.array(4, b.uint8)),
             ('pitch_eg_levels', b.array(4, b.uint8)),
             ('algorithm', b.uint8, {
                 '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
Ejemplo n.º 20
0
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
Ejemplo n.º 21
0
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
Ejemplo n.º 22
0
        with open(path, 'w') as f:
            f.write(x)


def hex_array(x):
    return str(map(hex, x))

nsf_head_spec = \
  [ ('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))
Ejemplo n.º 23
0
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))),
Ejemplo n.º 24
0
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