def init_struct_test(): mem = MockMemory() # setup init table init_tab = 0x100 mem_ptr = 0x200 ib = InitStructBuilder(mem, init_tab) ib.init_byte(0, 21) ib.init_word(12, 0xDEAD) ib.init_long(34, 0xCAFEBABE) ib.init_struct(ib.SIZE_LONG, 40, [23, 42]) ib.init_struct(ib.SIZE_WORD, 64, [11, 7]) ib.init_struct(ib.SIZE_BYTE, 80, [1, 2, 3]) ib.end() # build struct i = InitStruct(mem) i.init_struct(init_tab, mem_ptr, 32) # check struct assert mem.r8(mem_ptr + 0) == 21 assert mem.r16(mem_ptr + 12) == 0xDEAD assert mem.r32(mem_ptr + 34) == 0xCAFEBABE assert mem.r32(mem_ptr + 40) == 23 assert mem.r32(mem_ptr + 44) == 42 assert mem.r16(mem_ptr + 64) == 11 assert mem.r16(mem_ptr + 66) == 7 assert mem.r8(mem_ptr + 80) == 1 assert mem.r8(mem_ptr + 81) == 2 assert mem.r8(mem_ptr + 82) == 3
def init_struct_test(): mem = MockMemory() # setup init table init_tab = 0x100 mem_ptr = 0x200 ib = InitStructBuilder(mem, init_tab) ib.init_byte(0, 21) ib.init_word(12, 0xdead) ib.init_long(34, 0xcafebabe) ib.init_struct(ib.SIZE_LONG, 40, [23, 42]) ib.init_struct(ib.SIZE_WORD, 64, [11, 7]) ib.init_struct(ib.SIZE_BYTE, 80, [1, 2, 3]) ib.end() # build struct i = InitStruct(mem) i.init_struct(init_tab, mem_ptr, 32) # check struct assert mem.r8(mem_ptr + 0) == 21 assert mem.r16(mem_ptr + 12) == 0xdead assert mem.r32(mem_ptr + 34) == 0xcafebabe assert mem.r32(mem_ptr + 40) == 23 assert mem.r32(mem_ptr + 44) == 42 assert mem.r16(mem_ptr + 64) == 11 assert mem.r16(mem_ptr + 66) == 7 assert mem.r8(mem_ptr + 80) == 1 assert mem.r8(mem_ptr + 81) == 2 assert mem.r8(mem_ptr + 82) == 3
def mem_cache_rwx_write_test(): mem = MemoryCache(0x100, 0x210) # build cache contents mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xdead) assert mem.r16(0x200) == 0xdead mem.w32(0x300, 0xcafebabe) assert mem.r32(0x300) == 0xcafebabe mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # write to main mem main_mem = MockMemory() mem.write_cache(main_mem) # check main mem assert main_mem.r8(0x100) == 42 assert main_mem.r16(0x200) == 0xdead assert main_mem.r32(0x300) == 0xcafebabe assert main_mem.read(0, 0x101) == 43 assert main_mem.read(1, 0x202) == 0x1234 assert main_mem.read(2, 0x304) == 0x11223344
def mem_cache_rwx_write_test(): mem = MemoryCache(0x100, 0x210) # build cache contents mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xdead) assert mem.r16(0x200) == 0xdead mem.w32(0x300, 0xcafebabe) assert mem.r32(0x300) == 0xcafebabe mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # write to main mem main_mem = MockMemory() mem.write_cache(main_mem) # check main mem assert main_mem.r8(0x100) == 42 assert main_mem.r16(0x200) == 0xdead assert main_mem.r32(0x300) == 0xcafebabe assert main_mem.read(0, 0x101) == 43 assert main_mem.read(1, 0x202) == 0x1234 assert main_mem.read(2, 0x304) == 0x11223344
def mem_cache_rwx_read_test(): mem = MockMemory() # build main mem contents mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xDEAD) assert mem.r16(0x200) == 0xDEAD mem.w32(0x300, 0xCAFEBABE) assert mem.r32(0x300) == 0xCAFEBABE mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # write to cache cmem = MemoryCache(0x100, 0x210) cmem.read_cache(mem) # check cache mem assert cmem.r8(0x100) == 42 assert cmem.r16(0x200) == 0xDEAD assert cmem.r32(0x300) == 0xCAFEBABE assert cmem.read(0, 0x101) == 43 assert cmem.read(1, 0x202) == 0x1234 assert cmem.read(2, 0x304) == 0x11223344
def machine_mem_rw_test(): mem = MockMemory() mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xDEAD) assert mem.r16(0x200) == 0xDEAD mem.w32(0x300, 0xCAFEBABE) assert mem.r32(0x300) == 0xCAFEBABE mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # invalid values with pytest.raises(ValueError): mem.w8(0x100, 0x100) with pytest.raises(ValueError): mem.w8(0x100, -1) # invalid values with pytest.raises(struct.error): mem.w16(0x100, 0x10000) with pytest.raises(struct.error): mem.w16(0x100, -2) # invalid values with pytest.raises(struct.error): mem.w32(0x100, 0x100000000) with pytest.raises(struct.error): mem.w32(0x100, -3) # invalid type with pytest.raises(TypeError): mem.w8(0x100, "hello") # invalid type with pytest.raises(struct.error): mem.w16(0x100, "hello") # invalid type with pytest.raises(struct.error): mem.w32(0x100, "hello") # invalid width with pytest.raises(ValueError): mem.write(7, 0x202, 12) with pytest.raises(ValueError): mem.read(7, 0x202)
def machine_mem_rw_test(): mem = MockMemory() mem.w8(0x100, 42) assert mem.r8(0x100) == 42 mem.w16(0x200, 0xdead) assert mem.r16(0x200) == 0xdead mem.w32(0x300, 0xcafebabe) assert mem.r32(0x300) == 0xcafebabe mem.write(0, 0x101, 43) assert mem.read(0, 0x101) == 43 mem.write(1, 0x202, 0x1234) assert mem.read(1, 0x202) == 0x1234 mem.write(2, 0x304, 0x11223344) assert mem.read(2, 0x304) == 0x11223344 # invalid values with pytest.raises(ValueError): mem.w8(0x100, 0x100) with pytest.raises(ValueError): mem.w8(0x100, -1) # invalid values with pytest.raises(struct.error): mem.w16(0x100, 0x10000) with pytest.raises(struct.error): mem.w16(0x100, -2) # invalid values with pytest.raises(struct.error): mem.w32(0x100, 0x100000000) with pytest.raises(struct.error): mem.w32(0x100, -3) # invalid type with pytest.raises(ValueError): mem.w8(0x100, 'hello') # invalid type with pytest.raises(struct.error): mem.w16(0x100, 'hello') # invalid type with pytest.raises(struct.error): mem.w32(0x100, 'hello') # invalid width with pytest.raises(ValueError): mem.write(7, 0x202, 12) with pytest.raises(ValueError): mem.read(7, 0x202)