e0c8bce3e918773f231fbc9329d8a2e1d5731ff3
[libreriscv.git] / docs / pypowersim_tut.mdwn
1 # Pypowersim/ISACaller tutorial - In progress!
2
3 * Libre-SOC Wiki OpenPOWER ISA Pseudo-code: <https://libre-soc.org/openpower/isa/>
4 * PowerPC Arch assembler lecture: <https://www.eecs.umich.edu/courses/eecs373.w04/Lectures/stever_old_lectures/lec2.pdf>
5 * Hello world in PPC64 assembly: <https://github.com/matja/asm-examples/blob/master/ppc64/hello.ppc64.linux.syscall.gas.asm>
6 * Hello world in PPC64LE assembly: <https://gist.github.com/sandip4n/09b50786e88968faaecdf42360c85b1b>
7
8 This tutorial is intended to get started with Libre-SOC's in-house instruction
9 simulator. The main Python class doing the work is called `ISACaller`, while
10 the more comprehensive wrapper file used to run binaries is called
11 `pypowersim`.
12
13 ## `-->` START HERE `<--` Run `ISACaller` unit tests first!
14
15 TODO: Document tutorial.
16
17 Since `pypowersim` is much more involved (as it requires PC, register setup,
18 etc.), it is *strogly* encouraged to first write a basic unit test involving
19 the `ISACaller` class.
20
21 ### Find appropriate umbrella `test_caller_[FUNCTION].py` class
22
23 The directory `openpower-isa/src/openpower/decoder/isa/` contains a set of files
24 for running unit tests using pytest. If adding tests for a new category, a new
25 umbrella will need to be created.
26
27 The umbrella file has name of the form `test_caller_[FUNCTION].py`.
28 For example of the ALU test caller, see
29 [`test_caller_alu.py`](https://git.libre-soc.org/?p=openpower-isa.git;a=blob;f=src/openpower/decoder/isa/test_caller_alu.py;hb=HEAD).
30
31 It's suggested to copy the contents of an existing test caller file.
32
33 ### Write unit tests under the `src/openpower/test/[FUNCTION]/` directory
34
35 Once a test caller class exists, the actual tests reside under the
36 `src/openpower/test/[FUNCTION]/` directory. For example of the ALU tests, see
37 [alu_cases.py](https://git.libre-soc.org/?p=openpower-isa.git;a=blob;f=src/openpower/test/alu/alu_cases.py;hb=HEAD)
38
39 Copy an existing file when writing new tests, which should gradually teach you
40 how to use the `ISACaller` class.
41
42 ### Example of new test - system call instructions
43
44
45
46 ## Setup a Debian 10 chroot environment
47
48 **Skip this section if `pypowersim` is already present on your system.**
49
50 Setup new chroot:
51
52 $ cd dev-env-setup
53 $ sudo bash
54 # ./mk-deb-chroot isacaller
55 # ./mk-deb-chroot isacaller
56 # exit
57 $ schroot -c isacaller
58 (isacaller):$ cd dev-env-setup
59 (isacaller):$ sudo bash
60 (isacaller):# ./install-hdl-apt-reqs
61 (isacaller):# ./hdl-tools-yosys
62 (isacaller):# ./hdl-dev-repos
63 (isacaller):# ./binutils-gdb-install
64 (isacaller):# exit
65
66 *(NOTE to self: check if `hdl-dev-repos` actually necessary)*
67
68 From here on, `pypowersim` should be in your `$PATH` and can simply be called
69 from your terminal (when inside the newly created chroot).
70
71 (isacaller):$ pypowersim --help
72
73 Help message (may change, so try yourself):
74
75 -i --binary= raw (non-ELF) bare metal executable, loaded at 0x0
76 -a --listing= file containing bare-metal assembler (no macros)
77 -g --intregs= colon-separated file with GPR values
78 -f --fpregs= colon-separated file with FPR values
79 -s --spregs= colon-separated file with SPR values
80 -l --load= filename:address to load binary into memory
81 -d --dump= filename:address:len to binary save from memory
82 -q --qemu= run qemu co-simulation
83 -p --pc= set initial program counter
84 -h --help prints this message
85 notes:
86 load and dump may be given multiple times
87 load and dump must be 8-byte aligned sizes
88 loading SPRs accepts SPR names (e.g. LR, CTR, SRR0)
89 numbers may be integer, binary (0bNNN) or hex (0xMMM) but not FP
90 running ELF binaries: load SPRs, LR set to 0xffffffffffffffff
91 TODO: dump registers
92 TODO: load/dump PC, MSR, CR
93 TODO: print exec and sub-exec counters at end
94
95 ## Running existing example
96
97 To start with, let's see how a comprehensive example works. A good demonstrator
98 of the capabilities of SVP64 is the XChaCha20 encryption algorithm. (Difference
99 between ChaCha20 and XChaCha20 being an
100 [extended 192-bit nonce](https://crypto.stackexchange.com/a/101505)).
101
102 This page will go into the details of running the simulator, not the SVP64
103 specifics. Please see the SVP64 Cookbook page on
104 [ChaCha20](https://libre-soc.org/openpower/sv/cookbook/chacha20/)
105 for more detailed information on the algorithm and SVP64 features.
106
107 To run the example.
108
109 (isacaller):$ cd ~/src/openpower-isa/crypto/chacha20
110 (isacaller):$ make
111 (isacaller):$ ./test-chacha20
112
113 Or with `SILENCELOG=1` if you want less terminal output from the simulator:
114
115 (isacaller):$ SILENCELOG=1 ./test-chacha20
116
117 ## Explanation of the process
118
119 Konstantinos
120 [summarising the process](https://libre-soc.org/irclog/latest.log.html#t2023-09-10T18:44:49).
121
122