Skip to content

Commit

Permalink
Update handbook peripheral example
Browse files Browse the repository at this point in the history
  • Loading branch information
rawsample committed Jul 13, 2022
1 parent e2d02dd commit 974c6b3
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions handbook/0x03_memory.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ The example below provides an implementation of a HelloWorldPeripheral, which
returns another part of the string 'Hello World' upon every read.

```python
import struct

from avatar2 import *
from avatar2.peripherals.avatar_peripheral import AvatarPeripheral

class HelloWorldPeripheral(AvatarPeripheral):

Expand All @@ -105,7 +108,7 @@ class HelloWorldPeripheral(AvatarPeripheral):

# Convert the return value to an integer (py2/py3-compatible)
# Python >3.2 could just call int.from_bytes(ret, byteorder='little')
s2fmt = {1: 'B', 2: 'H', 4: 'I', 8: 'Q')
s2fmt = {1: 'B', 2: 'H', 4: 'I', 8: 'Q'}
ret = struct.unpack('<' + s2fmt[size], ret)[0]

return ret
Expand All @@ -121,7 +124,9 @@ class HelloWorldPeripheral(AvatarPeripheral):
self.read_handler[0:size] = self.hw_read
self.write_handler[0:size] = self.nop_write

[...]

avatar = Avatar()
qemu = avatar.add_target(QemuTarget)

hw = avatar.add_memory_range(0x40004c00, 0x100, name='hello_world',
emulate=HelloWorldPeripheral, permissions='rw-')
Expand Down

0 comments on commit 974c6b3

Please sign in to comment.