10. Setting up a toolchain

This lesson will assume you're on Linux. I think it will work on MacOS, but I'm not sure. You will need WSL if you're on Windows.

Until now, we have made our programs by hand-writing RISC-V assembly, or even raw machine code. That changes in this lesson, where we'll set up a toolchain for our processor. A toolchain is just a collection of tools that can be used together to write programs for a system. It often consists of a compiler, assembler, linker, and other related tools.

If you're creating binaries from one system for another system that has a different architecture, you'll probably need a cross compiler (although some compilers can compile for other architectures out-of-the-box, this is the exception, not the rule).

Low-level or embedded systems are usually programmed with C, and the most popular "brand" of toolchain is the GNU toolchain, containing GCC, probably the most popular C compiler. In this lesson, we'll see how to build binutils (a collection of tools for handling binaries) and set up a GCC cross-compiler. Both are part of the GNU toolchain.

On our "host" system (the system where we want to compiler the programs for our "target" system), we need to download and extract

The exact steps are outlined on the OS development wiki. I put them together in a small script:

export BINUTILS_VERSION="2.41"
export GCC_VERSION="13.2.0"

export PREFIX="$HOME/opt/cross"
export TARGET=riscv-elf
export PATH="$PREFIX/bin:$PATH"

export DOWNLOADS="$HOME/src"
export SOURCE="$HOME/src"

mkdir -p $SOURCE
mkdir -p $DOWNLOADS

cd $DOWNLOADS

wget https://ftp.gnu.org/gnu/binutils/binutils-${BINUTILS_VERSION}.tar.xz
wget https://ftp.gnu.org/gnu/gcc/gcc-${GCC_VERSION}/gcc-${GCC_VERSION}.tar.xz

tar -xf binutils-2.41.tar.xz
tar -xf gcc-13.2.0.tar.xz

cd $SOURCE
mkdir build-binutils
cd build-binutils
../binutils-${BINUTILS_VERSION}/configure --target=$TARGET --prefix="$PREFIX" --with-sysroot --disable-nls --disable-werror
make
make install

cd $SOURCE
mkdir build-gcc
cd build-gcc
../gcc-${GCC_VERSION}/configure --target=$TARGET --prefix="$PREFIX" --disable-nls --enable-languages=c,c++ --without-headers
make all-gcc
make all-target-libgcc
make install-gcc
make install-target-libgcc

This will download, extract, and compile GCC and binutils, and install them in ~/opt/cross/bin. For me, the script took about 20 minutes to execute. After it has finished we can add ~/opt/cross/bin to our PATH environment variable by adding something like this in our .bashrc (or similar file for whatever shell you use):

export PATH="$HOME/opt/cross/bin:$PATH"

Let's take the following test program:

#include <stdint.h>

uint32_t fib(uint32_t n) {
	if (n <= 1) return n;
	return fib(n - 2) + fib(n - 1);
}

int main(int argc, char *argv[]) {
	return fib(17);
}

Now, we can invoke the compiler as riscv-elf-gcc. However, we are compiling for a very bare-bones environment and need to take some precautions. For example, there is no C runtime on our CPU, which means we cannot use the C library. This means we have to tell the compiler to generate a "freestanding" binary (with the -ffreestanding flag), and not to include the C standard library (with the -nostdlib flag).

Execution of a C program starts at the _start function. We'll implement that in an assembly file, by just calling main and then hanging.

start.s:

.section .text
.global _start

_start:
call main
1: j 1b

Since our CPU just starts executing at address 0, we need to make sure that the _start symbol is placed at address 0. This can be done with a linker script.

linker.ld:

ENTRY(_start)
SECTIONS {
  . = 0x0;
  .text : {
    KEEP(*(.text._start))
    *(.text*)
  }
  .rodata : { *(.rodata*) }
  .data : { *(.data*) }
  .bss  : { *(.bss*)  }
}

We use the linker script with the -T linker.ld flag.

Further, we need to tell GCC what exact RISC-V architecture to target. We target RV32I, so we add the -march=rv32i flag. We also need to specify an ABI, because the default ABI (ilp32d) requires floating point registers from the D extension. So we set it to the ilp32 ABI that does not need the D extension with the -mabi=ilp32 flag.

All in all, we get

riscv-elf-gcc -march=rv32i -mabi=ilp32 -nostdlib -ffreestanding -O2 start.S main.c -T linker.ld -o prog.elf

This produces an output file prog.elf. This is an ELF file, a binary format commonly used on Linux. However, to execute an ELF file we need to "load" it: Parse the contents and put it in memory in the specified way. This is quite complicated and we want to just put the binary into memory. We can do this by extracting the binary with objcopy:

riscv-elf-objcopy -O binary prog.elf prog.bin

We now have a file containing the binary. The only thing that's left is to convert this binary to hexadecimal values which can be put in the VHDL source code. For this, I wrote a simple Python script.

process.py:

#!/usr/bin/env python

SIZE=4096
LINE=8
DEFAULTWORD = 'X"00000000"'

data=[]

inputdata = open('prog.bin','rb').read()
pad = (-len(inputdata)) % 4
inputdata += b'\x00' * pad
for i in range(0, len(inputdata), 4):
    w = inputdata[i:i+4]
    val = int.from_bytes(w, 'little')
    data.append(f'X"{val:08x}"')

oldsize = len(data)
for i in range(oldsize, SIZE):
    data.append(DEFAULTWORD)

i = 0
while i < SIZE:
    print(', '.join(data[i:i+LINE]) + ('' if i + LINE >= len(data) else ','))
    i += LINE

Now, we can compile a program as follows:

$ ls
compile.sh linker.ld main.c process.py start.s
$ ./compile.sh

This will dump a large amount of binary values to the screen; If we copy it and replace the contents of the RAM in bram.vhd with this, our program will be executed by our CPU.

toolchain/compile.sh ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+
3
+ riscv-elf-gcc -march=rv32i -mabi=ilp32 -nostdlib -ffreestanding -O2 start.s main.c -T linker.ld -o prog.elf
4
+ riscv-elf-objcopy -O binary prog.elf prog.bin
5
+ python process.py
toolchain/linker.ld ADDED
@@ -0,0 +1,12 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ENTRY(_start)
2
+ SECTIONS {
3
+ . = 0x0;
4
+ .text : {
5
+ KEEP(*(.text._start))
6
+ *(.text*)
7
+ }
8
+ .rodata : { *(.rodata*) }
9
+ .data : { *(.data*) }
10
+ .bss : { *(.bss*) }
11
+ }
12
+
toolchain/main.c ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #include <stdint.h>
2
+ #include <stdio.h>
3
+
4
+ #define LENGTH 4484
5
+ int16_t input[LENGTH] = { 17, 28, 8, -25, 33, -48, 2, 42, 39, -10, 29, -26, 24, 44, -37, -1, -4, 2, -43, -6, -44, 29, 47, -48, 13, -18, 18, 30, -44, 35, 18, 2, -38, 50, -28, 18, 21, 32, -8, 18, -33, 24, 46, -6, -9, 39, 7, 48, 41, -26, -52, 1, -1, -40, -60, 18, -18, -87, 73, -86, -45, 69, -24, -31, -69, -12, -79, 84, 83, 24, 70, -75, -81, -38, 24, 26, -26, -48, -52, -10, -90, 4, 54, -88, 31, -3, 91, -67, 65, -61, -32, 63, 83, -50, -90, 75, -26, -23, -26, -61, -89, -65, 250, 60, 652, -47, 88, 50, -61, -113, 36, 823, 26, -849, -46, 27, 58, 41, 68, -48, -42, 15, -22, 49, 78, 322, 28, -28, -95, -33, 56, -28, 37, 63, 13, -8, -57, 52, -64, -36, -8, -28, -313, 49, -87, -13, 24, 911, -35, -117, 60, -65, 34, -3, 52, 54, 85, -371, -67, -8, 668, -22, 96, 51, 8, -55, 69, 31, 83, -49, -734, 165, 35, 85, -45, 60, 332, 32, 36, -31, -22, 233, -702, 80, -23, 865, -6, -94, 57, 18, 225, 955, 34, -47, 55, -97, -9, 4, 63, 43, -1, 18, 7, 75, -224, -76, 10, 79, -898, -91, -47, -3, -74, 19, 50, -45, 46, 54, 36, 28, 8, -27, 55, 41, -41, 540, 60, 58, 76, -3, -831, -45, -78, -77, -2, -50, 24, -572, 3, -94, -509, -53, -35, 559, 21, -292, 10, 16, -98, 812, -40, 10, 74, 16, 255, 92, 670, -73, 56, -59, -41, -938, -62, -84, -5, 89, -23, 423, 62, -162, -57, -698, -80, 47, -413, -899, -69, 92, 322, -145, 59, 41, 39, -39, 9, -34, -28, -58, 559, -59, -89, -19, -13, 94, 34, 77, -2, -91, -86, -94, -67, -63, 430, -5, 5, 70, 30, -67, 67, -30, -39, -131, -21, 19, 98, -96, 36, 610, 54, -72, -90, -49, 11, -77, -93, -30, -5, 79, 67, 59, -29, 63, 66, -54, -546, -101, -28, 4, -6, 96, -1, 323, 435, -22, -74, -80, 54, 658, 88, -46, 21, -23, -89, -84, 72, 3, 60, 40, 48, -49, 1, -5, -195, 46, -20, 686, 88, -80, 50, 81, -31, -67, 15, -398, -269, -1, -722, -978, 53, -153, 39, 61, 7, 517, -24, -19, 19, 45, 135, 70, 950, 67, -267, -672, 41, 896, -65, -33, 35, 883, -72, -27, -86, -81, 81, -87, 13, 88, -48, 52, -47, -52, 117, -653, -36, -88, 12, -71, 25, 64, 751, -7, -33, 598, -98, 54, 30, 766, 15, -60, 695, -1, -56, 39, 16, -98, 101, 99, -277, -564, 41, -21, -79, -23, 777, 246, 37, 563, 40, 784, -80, 56, -69, -31, 3, 23, -331, -95, -563, 63, -5, -84, -48, 16, 84, -63, 78, -17, -61, -30, 3, 301, 26, 128, -988, -13, -14, 87, 15, 85, -61, 48, -587, 65, 72, 63, -754, 733, -79, 584, -81, 597, -94, -40, -66, -94, 94, 15, -15, 24, 67, 807, 82, 86, -66, 20, -41, 95, 26, 70, 430, -3, 3, -47, 31, -890, 513, -7, 61, 39, -75, 886, -97, 86, 60, 140, -87, 49, 46, 23, -4, 10, -276, -61, 24, 76, -59, -42, -530, -8, 56, -696, -83, -38, -290, 57, -39, 99, 71, 55, -381, 28, -75, -26, 22, 77, -4, -94, 68, -68, -246, 11, -65, -54, -46, -58, 56, -98, 94, -94, -429, 29, 867, 133, 41, 17, -58, -9, 920, -12, 1, -28, -72, 769, 31, 18, 82, 33, 518, -375, -78, -98, 92, -16, -44, 31, -51, 65, 40, -17, 52, -879, 3, 14, 382, 28, 38, 23, 539, 869, -46, -23, -87, -3, 790, 878, 563, -41, -67, 50, 20, 55, 42, -47, -44, -78, -675, 44, -578, -22, -821, 21, 28, 18, -682, 42, 52, 8, -66, -986, -80, -67, 6, -38, -80, -38, -85, -32, -17, 95, -3, 25, -19, 19, -56, 178, -22, 55, -355, -966, 810, -8, -388, -56, 485, 51, -28, -50, -79, -64, -32, -52, -323, 61, -322, -579, -37, 477, 508, 46, 972, -607, -919, -85, -521, 25, 49, 32, 23, 477, 71, 92, -54, -468, 59, -5, 294, -274, 20, 65, 287, 13, -856, -71, -344, 71, 77, -56, 90, 333, 56, -79, 4, 286, 89, -78, 20, 58, -313, 822, -5, 5, 92, 82, 17, 41, -38, 50, -71, -882, -75, 75, 298, -43, 45, 5, 21, 74, -55, 58, -36, 633, 57, 43, 15, -8, 93, 376, -781, 50, 55, -68, 56, -17, -71, 52, 435, 73, 69, -70, 97, 850, 44, -50, -8, 62, 18, -272, 15, 85, -14, 214, 46, -46, -51, 751, 37, 363, -18, -82, 288, -68, -94, -3, 2, 82, -21, 32, 82, -37, 37, -33, 81, 52, 51, 74, 75, -51, -44, 52, 43, 547, 753, 185, 78, 83, 54, -22, 25, -71, -32, -47, 60, 46, 80, -81, -35, 894, 14, 69, -54, 74, -24, -6, 5, -565, -30, -93, -7, 28, -85, -39, -63, 59, -30, -345, -674, 75, -354, -908, 622, 10, 4, -48, 55, -2, 99, -3, -1, -31, -54, 60, 3, 84, 40, 228, 92, -95, -177, -50, -71, 60, -38, 27, 75, 47, -126, 12, 287, 71, 95, -39, -9, 441, -92, -158, 36, 5, 49, 51, 877, -503, 241, -38, 74, 975, 49, -17, 19, -53, 53, 26, -41, -85, 22, 260, -7, -19, -70, -48, 270, 9, 71, -88, -87, 87, 2, 14, 84, -67, -33, -38, 75, -37, 13, -13, 43, -1, 51, 7, 63, -29, 15, -563, 58, -19, 50, -275, -10, 71, 42, -83, -20, -62, 88, -26, -99, -1, 93, -95, -36, -62, 29, -61, 43, 89, -10, 65, 781, 64, 84, 16, 76, 19, 98, -730, -63, 921, 83, -4, -16, -84, 77, -57, 91, 46, 89, -546, 17, 55, 51, -31, -92, -63, 47, 16, 20, 23, -43, 18, 20, -35, 97, 662, 28, 26, -16, -18, 88, 4, 29, -3, -53, 132, 21, -36, -59, 95, 15, -70, -80, 35, 66, -66, 63, 27, -358, 68, -23, 423, 62, -25, 63, -212, 12, 50, -27, 16, 23, 36, -98, -13, 85, 52, -302, 78, 398, -198, 96, -50, -46, 82, -129, -80, -13, 5, -73, -89, -58, -335, 25, 88, -164, -15, 61, 95, 69, 1, 30, 272, -221, -79, 165, 28, 43, 6, -96, 82, 82, -582, 18, 82, -637, -70, -93, -471, -633, -41, 97, -252, 59, 63, -38, 16, -41, 61, 80, 337, 94, 69, -58, 36, 15, 83, 924, 62, -48, -14, -40, -43, 46, 67, -33, -20, 23, 28, -737, 9, 77, -77, 53, 34, -87, -70, 70, 67, -49, 979, -87, -94, 45, -701, -18, 58, 59, -40, 81, 76, 243, 77, 540, -5, 869, 12, 901, 87, 40, -640, 3, -522, -134, -47, 54, -54, 884, -19, -265, 14, 143, 43, -629, 19, 86, 78, -47, -7, -59, 55, 11, -402, -29, 265, -90, -62, 11, 99, 101, -852, -48, 92, 81, -73, -41, 1, 55, -64, -20, -31, -169, -192, 65, 96, 532, 90, 15, 33, 236, 16, 94, 63, 2, 19, 949, 51, 20, 6, -71, 45, 840, -2, 92, 670, -26, 426, 18, -55, 63, -802, 89, 91, -65, 60, 1, -95, -97, -8, 15, -19, 58, 78, -32, -89, 27, -48, 68, -91, -673, 207, -2, -999, -2, -998, -66, 66, 22, 30, -35, 83, -93, -150, 5, -39, -23, -88, -28, 87, -471, 68, -468, -83, -17, -85, -39, 71, -947, 83, -33, 36, 89, 25, 897, -57, -40, 67, 733, -19, -756, 81, 849, 53, -89, -50, 35, -226, 618, -68, -82, -946, -742, -4, -58, -98, 13, 70, -81, 85, 15, -10, -41, 43, 968, 194, 46, 578, 22, -58, -763, 99, 53, -10, -21, 14, 386, 26, -25, -1, -270, -89, -20, 79, 66, -85, 236, -1, 84, -45, 68, -23, 54, -54, -9, 63, -54, 574, 6, 190, -70, 186, 81, -67, -202, 121, 781, -94, 94, -727, -280, -80, 87, 97, 82, -66, 87, 461, -761, -37, -648, 43, -120, 60, 95, 83, -43, 48, -6, 25, -9, -73, -738, -80, 17, -17, -16, 2, -11, -61, 945, -59, -402, 72, 519, -90, -37, 859, -26, 92, 13, -59, -270, 713, -84, 65, 35, -54, 54, 81, -201, -236, 89, -33, 18, 82, -13, 321, -8, -192, -8, 75, 25, 56, 44, -5, 880, 404, 17, 4, -49, -51, 10, -10, 72, 50, -3, -139, -80, 974, 59, 698, 41, 90, -891, -53, -18, 85, -79, 13, -619, -546, 71, 806, 69, -20, -80, -86, 5, 81, -64, 64, 12, 88, -331, 17, -3, 33, 279, 21, -659, -7, -25, 75, 732, 75, 493, 9, -639, 114, -84, 82, 518, -135, -82, 90, 482, -724, 7, -31, -46, -12, 92, 33, -40, 189, 77, -746, 34, 412, -9, 954, -945, -3, -40, 43, 20, 80, -81, 6, 16, -432, -9, -6, -94, -88, -43, -69, 91, 930, 29, -625, -98, 73, -86, 20, 557, 9, -55, 40, 15, -42, 42, 63, 68, 57, 58, 26, -93, -60, 5, 6, -35, -68, -27, -87, 54, -81, 14, -130, -15, 650, 95, -22, -278, -79, -321, 64, 36, 96, -46, 5, -59, 62, 42, 82, 13, 305, -62, -38, 15, 85, 15, -172, -129, -4, 90, -62, -38, -83, -32, -85, 89, -89, 963, -13, 85, 65, -98, -43, 72, -759, 52, 3, -975, 59, -11, -79, -221, 90, 29, 81, -19, 658, 71, 28, -38, -45, 45, -197, 7, -10, 96, 889, -85, 717, 59, -76, 797, 3, -24, 24, -24, 305, 65, -46, -623, -77, 429, -29, 13, -131, -82, 14, -85, 266, -53, -82, 56, -7, 91, 533, 67, -45, -75, -92, 103, -5, 14, 19, 90, -409, 938, -38, 58, 72, 57, 73, 48, -55, 21, 34, -52, 860, -8, -908, -64, -84, -52, 11, 77, -9, 21, -97, 97, -38, 38, -981, -56, 85, -6, -42, -231, 29, -88, 89, 1, 376, 24, 27, -27, 86, 14, 64, 96, 1, 39, 31, -9, 65, -91, -95, -901, 644, 56, 9, 8, -17, 21, 9, 79, 91, -289, 62, 71, 21, 35, -719, 22, 597, -11, 86, 88, 73, 32, 32, -84, 82, 2, 3, 9, -12, -32, 22, -90, -53, 53, -17, -83, 42, 30, -72, 38, -936, -502, 621, -25, 65, 935, 904, 90, -90, -53, -47, -19, 39, -41, 557, -62, 26, 316, -23, -32, 39, 215, -15, -45, 23, 33, -47, -78, -16, 20, -944, 54, -19, 19, -25, -23, -52, 347, 53, 96, 603, -99, -20, 999, 87, -66, -2, 23, 76, 3, -4, 69, -565, 41, 745, -852, 66, -2, 503, 99, -990, 90, 310, -5, 94, -36, 6, -605, 61, -26, 607, -93, 2, 985, -915, 15, -699, -1, 83, -87, -296, 49, 51, 69, -363, -734, 28, 8, -35, -556, 12, -29, 635, -36, 1, 46, -98, 52, -27, -643, 70, 739, 24, -4, -959, 470, 30, -46, -54, -44, 79, -535, 67, 676, -43, 98, -90, -8, 44, -52, -92, -799, -91, -610, -2, 2, 52, 45, 123, -20, 66, 67, 67, 90, 946, 91, 39, 69, 65, -51, 61, 46, -56, 88, -88, -31, -5, -76, 212, -439, -61, 99, 1, -49, -39, -54, -53, 70, -664, 569, 61, -9, 68, 81, 19, 98, -62, -50, 51, -10, -43, 485, -51, 82, -246, -554, -85, 14, 53, 47, -1, 72, -66, 66, 95, 87, -82, -68, -9, 70, -375, 37, 45, 51, 649, 90, -23, -31, -636, 80, 20, -93, -207, -32, 32, -81, -22, 3, -88, -22, 79, 31, -108, 8, 340, -81, 141, 73, 27, 309, 91, -88, -112, 79, 83, 307, -469, 715, 85, 63, -25, 9, -76, -789, 56, -38, -270, -7, -23, -97, -60, -43, 31, -31, 72, 967, -39, 98, -23, 57, -74, 98, 97, 747, 12, 56, -68, 61, -61, -49, 530, 66, -49, -98, 36, 64, 31, 18, 851, -77, 77, 212, -24, -88, 11, 8, -839, -18, -78, -84, 22, -22, 56, -56, 47, -47, 72, -72, -84, -70, -46, 21, 18, -277, -60, -2, -23, 29, -28, -78, -19, 22, -70, 71, 996, 86, 14, 26, 74, 21, 79, -57, -13, -50, -44, 64, 15, -115, -32, -27, -139, -902, -62, -81, 56, 4, 77, 60, -81, -73, 94, -94, -37, 229, 69, -25, -53, -83, 574, 857, -31, -127, -573, 508, 92, -746, -654, -51, 12, 75, -36, 768, 310, 30, 85, -46, -847, -80, -62, 42, 311, 259, 793, -46, 81, 33, 747, 422, -75, 75, 6, -6, 41, -949, -92, -101, 68, -75, 28, 465, -54, 66, -97, -55, 50, 623, -88, -936, -94, -35, -570, 5, -61, 67, 62, -36, 56, -88, 43, 11, -81, -47, -20, -506, 16, -16, 727, -55, -34, -840, -72, -19, -7, 96, -96, -23, 23, 54, -94, -94, -28, -48, -5, -8, 11, -88, -20, 620, 18, -34, 16, 104, 96, 38, 54, 408, -7, -24, -20, 91, -72, 883, -51, 63, -174, -89, 8, -8, 446, 68, -81, -46, -401, -77, -9, -86, -26, -792, 77, 163, 821, -848, 91, -642, 60, 29, -83, 71, -506, -53, -76, 93, -142, -951, -6, -91, 15, 77, -95, 72, -776, -96, -62, -55, 850, -72, -64, 3, -2, 2, 93, 39, -68, 36, 8, -57, -33, 82, -477, 77, 437, 63, -94, 59, 2, -40, -34, -65, -25, 97, 73, 27, -1, -14, -84, 724, 75, -91, 91, 363, -63, 297, -29, -79, 511, 366, 34, 34, 27, 60, 49, -370, 274, 50, -124, -5, 5, -5, 5, 91, -46, -57, -88, -338, -30, -6, 66, 77, 31, -43, -57, 18, 79, 5, -34, -68, 550, 199, 51, 495, -95, 74, -51, -23, 31, -83, -99, 723, -49, -71, -28, -124, 76, -72, 96, -18, 580, -99, -33, 70, 65, -39, 74, -91, -470, 96, 65, -20, -80, -19, -81, -36, 29, -93, 90, 44, 29, 209, 28, 3, -1, -45, -107, -33, 96, 14, 8, 265, -43, -33, -24, 98, 2, 537, -411, 77, -93, 290, -332, 20, 12, -84, 884, -39, -706, 8, 16, -79, 23, -23, -728, -72, 5, -5, -97, 51, -34, -20, 57, -62, 305, -51, 5, -496, -14, 40, 654, -38, 71, -89, 18, 183, 17, -66, 4, 62, -75, 15, 60, 21, 79, 32, -73, -7, 52, -313, -8, -6, -853, -7, -17, 72, -72, -14, -50, 51, -91, 91, 18, -12, -93, -64, -36, -23, 36, -13, -26, -62, -5, 293, -943, -50, 17, 79, 95, -21, 86, -251, 8, -82, 362, 3, 60, -63, 48, 1, 11, -60, -52, -229, -83, 43, -79, -20, -5, -75, 927, -27, -82, 266, -14, 30, 63, -34, 171, -32, -68, 93, 7, -12, 177, -86, 21, -31, -69, -318, -85, -71, 70, 72, -34, -97, 80, -117, 9, 91, 99, -99, -64, 74, -51, -60, 79, -42, 818, -54, 614, -695, -19, -979, -35, -486, -51, 81, -28, -92, 19, -29, 13, -82, 12, 99, -95, 53, 293, -95, 38, -53, -302, 19, 10, -57, 47, 25, 74, 53, -65, 13, -41, -68, -67, -70, -25, -50, -90, -89, 763, 863, -48, -67, 82, -58, -98, -537, -95, 37, 45, 716, -26, -98, -279, -66, -59, -75, -24, 424, 30, 15, -45, -70, -77, -353, 25, 35, -56, 96, -49, -751, 21, 47, -43, -67, -58, 72, 31, -86, 54, 29, 868, -68, 25, 90, 42, 54, 89, -27, -73, 33, 412, 62, -87, -47, 22, -53, -66, 96, -73, 1, -99, 20, 784, 795, 13, -613, -708, -61, 69, 17, 83, -54, -546, 799, 801, -84, -31, -78, -22, 89, -43, 74, 59, -64, -911, 53, 11, 347, 11, 14, 68, 7, -31, -69, -501, -98, 99, -85, -388, 40, 8, 389, 5, 65, 871, -798, -61, 54, 26, -26, 47, -33, 95, -83, 804, 26, 948, -9, 5, 75, -12, -18, -45, -18, 9, 9, 266, 34, 55, 83, -94, 41, -185, -47, -74, -79, 91, -7, -84, -36, -964, 46, -546, -9, 9, 89, 40, -1, -53, -475, 70, -8, -62, 40, 60, 60, 91, 49, -53, 34, 19, -10, 56, -50, -34, 538, -92, -908, 12, -12, -90, 88, 3, 2, 56, -65, 6, -65, 317, -152, -7, 7, -60, 56, 25, -992, 47, -13, 48, -52, -656, 15, -753, 57, 701, 82, -45, 467, 19, 54, -24, 32, -37, -2, 31, 61, 91, -521, 58, -242, 89, 90, -46, 920, 77, -77, 5, 82, -87, -404, -16, -44, -48, 112, 416, -216, -7, -6, 522, 91, -25, -437, -38, -892, -74, -634, -31, 36, -63, 18, 40, -4, 504, 40, -40, 3, 777, -455, 75, 87, -4, -83, 58, -747, -69, 58, 19, 81, -10, -90, -66, -50, -2, -82, 7, -7, -366, 866, -37, 27, -90, 2, -202, 79, 906, -16, 31, -52, 64, -84, 36, 710, -40, -19, 22, -611, 57, -53, 64, 14, -8, 6, 76, 589, 33, 48, 748, -11, 96, -85, 12, -46, -94, 70, 55, 273, 67, 881, 87, -4, -1, -97, -90, 87, 4, 96, -62, 62, -78, 28, 1, -67, 216, -19, 19, 39, 75, -714, 97, 3, -99, -96, -8, -499, 47, -57, 41, -73, 33, 14, 1, -45, 63, -86, -47, 20, 91, 98, 102, 87, -22, 36, 86, 24, -211, -217, 44, 30, 42, -98, 199, -34, 34, 50, 90, 28, -32, 64, -57, 27, 105, 47, -22, -35, 301, -49, -17, -22, -778, -606, 6, -25, 708, 36, -79, 4, -31, -13, -92, 392, 22, 78, 63, 81, -2, 8, 49, -808, -37, 67, -507, 86, -20, -75, -260, 26, -79, -39, 47, -65, 806, 33, -57, -17, 31, 96, -15, 54, 56, 608, 997, 42, 31, 31, 69, -84, -516, -81, 11, -33, 3, 802, -642, 40, -74, -49, -91, -886, 89, 11, -18, -82, -26, -252, -40, -98, -79, -60, -845, -1, 1, -25, 24, 807, -55, 11, -66, -73, 72, 5, 513, -68, 485, 70, -81, -19, -86, -72, 46, -88, -59, -41, 373, -73, -49, -751, 430, 70, 74, 726, -5, -95, 67, -666, 699, -67, 70, -82, 19, 360, 77, -899, -68, 66, -376, -78, 78, 97, -97, -58, -73, 86, -55, -9, -91, 78, 5, -38, 15, 639, 1, 51, 66, -84, 59, 8, -334, -55, 72, -82, -93, 92, 330, -94, 64, -487, 87, 65, -566, -670, 24, 10, 42, 95, 38, 366, -4, 29, -29, 91, -94, 48, -660, -40, 86, 769, 904, -4, -76, 78, -45, -73, -14, -70, -58, -69, -873, -234, -38, -90, 46, -98, -93, 37, -18, 88, -44, -553, 97, -469, -57, -58, -42, 37, 370, -331, 299, -49, -85, 85, 180, 918, 302, -128, -72, -11, -26, -63, -25, 25, 5, -47, -14, 56, 69, 525, -994, 533, -633, -1, -53, -46, -10, 98, -53, 78, -13, 66, -66, 49, -49, -9, -66, 18, 857, 72, -72, 189, 11, -57, 29, 81, -51, -702, 1, 45, -62, -29, -9, 54, 64, -330, -749, -85, -8, -81, -309, -388, 82, -88, 92, -58, 98, 43, -49, -934, 894, -94, -47, -34, -819, -741, -859, 6, 92, -73, 75, 21, -965, -90, 34, 252, -588, -64, -738, -96, -66, 66, -28, -19, -65, -86, 32, 21, 457, 22, -356, 378, 635, 31, -67, 79, 96, 604, -65, 43, 47, -65, -60, 975, -75, -30, -70, -53, -73, 91, -50, -927, -51, -41, 49, -89, 41, 3, -680, 829, 335, -84, 21, 553, -774, 90, -90, -90, 11, 49, -781, -82, 93, 267, 82, 51, 22, 78, 97, 813, 990, 34, -34, 82, 77, 69, -63, -114, -85, 7, -73, 27, -11, 5, 79, 88, 12, 95, -815, -80, -952, -748, -375, -87, 62, -79, -21, -49, 49, 48, -48, 2, -2, -328, 6, 9, -98, 29, -18, 35, 66, -77, 76, 58, -58, 540, 12, 48, 687, 65, -937, -13, -2, -378, -98, 57, -985, -23, -9, -64, -90, -10, 28, 172, -97, 450, 3, 8, 6, 88, 92, 60, 66, 29, 82, 59, 44, 48, -99, 60, -369, 88, 22, -66, -63, 89, 40, -24, 58, 26, -234, 83, 688, -37, 87, -987, 55, -55, 35, 11, 78, -12, -95, 25, 58, 49, 77, 74, -266, 66, 99, 11, -777, 58, 54, -7, 62, 77, 59, -84, 48, 41, -41, 68, -67, -29, 74, 54, -83, -90, -92, -87, 52, -32, 934, 57, -70, 21, 46, 44, 52, -52, -147, 94, 69, -19, -47, -89, -95, -57, 16, 60, 92, 72, 16, 45, 80, -90, 245, -69, 124, 58, -11, 71, -78, -40, 94, -83, -11, -43, -39, -50, 30, -54, -7, 932, -769, 34, 266, -80, -276, -2, 758, 22, -622, -701, 50, 63, -12, 58, 543, 99, -6, 6, 808, -587, 102, -95, -55, -34, 570, -9, -16, 2, -20, -63, 30, 67, 7, -69, 64, 98, -3, 3, 13, 64, -241, -4, 68, -62, -6, 68, 49, -84, 35, -23, 23, -85, -15, 52, 548, -35, 48, -52, 571, -432, -59, -41, -11, 1, 10, -79, -60, 39, -49, -950, 14, 94, -51, 42, -678, -22, -60, 82, 78, -41, 885, 74, -61, -23, 39, -44, -43, 14, -9, -91, -69, -31, 45, -80, 77, -72, -76, 78, -72, 87, 113, -40, -761, -9, 91, 82, 78, 41, 424, 24, -39, 9, -15, 15, -84, -16, 290, 899, -37, 54, 79, -25, -63, -61, -36, 55, -65, -94, 51, 53, -20, -71, 91, -16, -84, -798, 16, -11, -7, -95, 533, 81, -33, -22, -64, -5, -605, 10, 67, -36, -31, -13, 92, -421, 91, -19, 501, 43, 72, 6, 48, 31, 69, -13, 48, 89, 76, -18, -33, 75, -24, 32, -32, 11, 821, -32, 81, 19, 304, 296, -2, -98, -50, -11, -51, 972, -32, 87, -15, -601, 646, 80, 75, 680, -80, 11, 89, 13, -85, 42, 97, 896, 27, 71, -62, 180, 61, 360, 552, 65, 171, 12, -68, 68, 30, -830, 90, 10, -81, -56, -163, -28, 45, 11, 32, -16, -44, -161, -6, -90, 57, 59, -59, -54, -897, -43, 72, 584, 38, -64, 39, 23, 902, 74, 40, -93, -821, 46, -46, -62, -52, -86, 56, 44, 71, 336, 93, 151, 64, -61, -554, 50, -37, 11, -624, 68, -68, -42, 8, -54, -992, 28, 638, -585, 11, -16, -30, -66, -27, 27, 92, -93, 43, 458, 12, 88, -85, -26, -2, -87, 138, 62, 682, -82, 8, -47, 15, -58, 7, 67, 27, 33, 48, -194, -485, 293, -44, 30, 4, -4, 53, 38, 10, 96, -14, -83, -10, -31, 23, -10, 38, 2, 88, 52, 21, 27, 270, 42, -92, 73, 823, -590, 47, -79, 7, -79, -26, 626, -790, -73, -67, -50, -442, -38, 15, -51, 74, 51, -46, -97, -8, -40, 340, -61, 61, -35, -65, -511, 40, 58, 62, -19, 70, -798, -27, 748, 68, 864, 45, -367, 67, -25, 62, 63, -13, -75, -352, -62, 85, -83, 81, 39, -41, -79, -2, 2, -265, -83, -52, -84, -16, -36, 435, 27, -70, -50, 37, -14, 71, -6, -94, 302, -333, 31, -46, 46, -34, 134, -89, 95, 94, 2, -2, 8, 92, 365, 35, -225, -75, -12, -47, 559, 65, 51, -16, 76, 24, -47, 97, 50, -28, -889, -2, -19, -62, -63, -456, -81, -89, 486, 31, 724, 48, -74, 31, 69, -26, 301, 31, 652, 42, -261, 35, 50, 79, 171, -7, 7, -386, 86, 26, 395, -48, -55, -18, 555, 23, 59, -37, -25, -64, -51, 3, 837, -29, 229, 91, -406, 70, 99, -978, -76, -19, 89, 78, 47, 22, -17, 47, 53, 13, 53, 87, 4, -257, 586, -63, -463, -60, 5, 95, 66, 34, -195, 47, 6, 2, -39, -21, 56, 44, -83, -61, -770, 547, 5, -53, -66, 881, 85, 615, 19, -13, -179, -47, 320, -53, -41, 94, -95, 78, -819, 36, -36, 969, -9, -44, -89, -36, -7, -25, 8, 69, -41, -94, 769, 866, 907, 39, 3, -17, -86, 50, -50, -51, -718, 23, 50, 50, 230, 70, -94, -60, -914, 68, -36, 56, -20, 37, -37, 382, -11, 895, -66, -51, 87, -39, -9, -88, 27, -52, -24, 672, 5, -96, 74, -89, 993, -12, 28, 39, -465, 12, 31, 22, -65, -32, -865, -89, -77, -37, 9, -9, -37, -378, -85, -14, -86, -45, 45, 40, -40, -40, 507, -88, -59, -85, 65, -99, 929, -30, -75, 647, -72, 301, 40, -24, -81, 54, 10, -93, -2, 23, 92, 657, -73, 16, -79, -341, -941, 45, -4, -14, -19, 75, -42, 69, -69, -35, 20, -593, -93, -607, 8, 4, 167, 29, 52, -52, 574, -34, 53, 7, 808, 98, -499, 45, 8, -52, 16, -52, -73, 812, -11, 18, 63, -72, -9, 3, -3, 92, 17, -809, 44, 56, -318, -61, -68, -93, -9, 12, 46, 83, 51, 510, 47, 7, -307, 62, 86, -48, -71, -222, -6, -8, -82, -33, 22, -37, 47, -6, 14, 86, 96, 147, -47, -15, 66, -98, 77, 61, 98, -789, 82, -978, -804, 79, -79, 97, 903, -43, 717, 26, 32, -82, -708, -42, -380, 80, 57, 42, 61, -60, -96, -4, 34, -14, -14, -3, -203, 61, -45, -16, 299, 29, -28, 8, -11, -354, -74, -22, -75, 28, -10, -90, 99, -12, 13, -179, 48, -73, -46, -83, 43, -10, 73, 27, 52, -83, -69, -83, -80, -98, -495, -7, -37, 446, -14, 68, -65, 65, -30, 111, 67, -95, 94, 91, -84, 92, -44, -491, -11, -5, 96, 9, 352, -34, 12, 13, -98, -31, 86, -3, 3, 83, -54, 71, 743, -43, 44, -69, 25, -90, 440, -96, -524, 70, -33, 933, -819, 4, -18, 57, 76, -254, -8, -38, -49, -53, -33, -76, -22, -867, -37, -68, 5, -392, -39, -97, -72, -52, -848, -14, -83, -91, 88, -24, 24, 285, 50, -49, 14, 28, 85, 605, 47, -93, 43, -15, 56, 44, 33, 83, 12, -92, -956, 81, -61, -244, 31, 31, 27, 10, -55, -59, -8, 44, -77, 834, 66, 50, -50, 7, 33, -40, -43, -37, 63, 8, -204, 613, 21, 879, 32, 856, 12, 45, -18, -98, 24, -53, -82, -49, 88, -57, 2, 36, -80, 24, -82, 68, -773, 5, -26, -815, -638, 968, -24, 835, -93, -95, 88, 27, -23, 76, 596, 24, 61, -62, 1, -2, -98, -613, 2, -66, 37, -60, -35, -79, -47, -242, 93, 64, 94, -67, 19, -28, 63, -35, -83, -432, -85, 64, 36, -871, -29, 542, -42, -975, -88, 5, -45, -3, 72, -19, -25, 78, -95, -54, 65, 57, 27, -56, -44, 42, 29, 29, 37, -89, 63, 5, -72, 41, 15, -12, 45, 27, 43, 97, -16, 17, -68, 67, 21, 12, -2, 50, 92, 4, 56, 33, 68, 2, -36, -41, -99, -19, 34, -9, -86, 20, 81, 32, 21, -41, -19, -18, 3, -12, -13, 42, 31, 17, -32, -2, -39, 41, -25, -44, -29, -29, 43, 45, 34, -26, 50, -32, -38, 22, 19, -45, 41, 9, 33, -27, 16, -9, -10, -8, -7, -40, 15, -23, -46, -42, 33, -18, -45, -2, -8, 14, -15, -26, 15, -39 };
6
+
7
+ int main(int argc, char *argv[]) {
8
+ int32_t dial = 50, n = 0;
9
+ for (int i = 0; i < LENGTH; i++) {
10
+ if (dial == 0 && input[i] < 0) dial = 100;
11
+ if (dial == 100 && input [i] > 0) dial = 0;
12
+
13
+ dial += input[i];
14
+ while (dial < 0) { dial += 100; n++; }
15
+ while (dial > 100) { dial -= 100; n++; }
16
+ if (dial == 0 || dial == 100) n++;
17
+ }
18
+ return n;
19
+ }
20
+
toolchain/process.py ADDED
@@ -0,0 +1,24 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python
2
+
3
+ SIZE=4096
4
+ LINE=8
5
+ DEFAULTWORD = 'X"00000000"'
6
+
7
+ data=[]
8
+
9
+ inputdata = open('prog.bin','rb').read()
10
+ pad = (-len(inputdata)) % 4
11
+ inputdata += b'\x00' * pad
12
+ for i in range(0, len(inputdata), 4):
13
+ w = inputdata[i:i+4]
14
+ val = int.from_bytes(w, 'little')
15
+ data.append(f'X"{val:08x}"')
16
+
17
+ oldsize = len(data)
18
+ for i in range(oldsize, SIZE):
19
+ data.append(DEFAULTWORD)
20
+
21
+ i = 0
22
+ while i < SIZE:
23
+ print(', '.join(data[i:i+LINE]) + ('' if i + LINE >= len(data) else ','))
24
+ i += LINE
toolchain/start.s ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ .section .text,"ax",@progbits
2
+ .global _start
3
+ .type _start, @function
4
+
5
+ _start:
6
+ call main
7
+ .p2align 2
8
+ .word 0x0000006f