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