# Assignment3: SoftCPU
# 1.Set Environment
### 1-1. Get xPack GNU RISC-V Embedded GCC
[xPack GNU RISC-V Embedded GCC](https://github.com/xpack-dev-tools/riscv-none-embed-gcc-xpack/).
```bash=
#download
cd /tmp
$ wget https://github.com/xpack-dev-tools/riscv-none-embed-gcc-xpack/releases/download/v10.2.0-1.1/xpack-riscv-none-embed-gcc-10.2.0-1.1-linux-x64.tar.gz
$ tar zxvf xpack-riscv-none-embed-gcc-10.2.0-1.1-linux-x64.tar.gz
$ cp -af xpack-riscv-none-embed-gcc-10.2.0-1.1 $HOME/riscv-none-embed-gcc
#Make path
$ cd $HOME/riscv-none-embed-gcc
$ echo "export PATH=`pwd`/bin:$PATH" > setenv
$ cd $HOME
$ source riscv-none-embed-gcc/setenv
# Check
$ echo $PATH
/home/leonard/bin:/home/leonard/bin:/home/leonard/riscv-none-embed-gcc/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
```
### 1-2.Install srv32
```
$ cd #HOME
$ git clone https://github.com/sysprog21/srv32
```
#### Important thing after install srv32
```bash==
cd srv32
make all
```
### 1-3.Download GTKWave
[GTKWave download site](http://gtkwave.sourceforge.net)
## 2.Flow Chart
---

## 3.Start compile
My work in [Asignment 1](https://hackmd.io/AYDM9UtgTcGFExkdaMKGhw?view) including 3 ways of reversing string written in risc-v
create a file for `hw`
and copy makefile in other folder into it
Edit `SRC` in makefile
## C code Implementation
:::spoiler {state="open"}1.original
```c=
#include <stdio.h>
void reverseString(char* s,int sSize)
{
char tem;
for(int i=0;i<=sSize/2-1;i++)
{
tem=s[i];
s[i]=s[sSize-i-1];
s[sSize-i-1]=tem;
}
}
}
int main()
{ volatile char* tx = (volatile char*) 0x40002000;
char s[] = "Hello";
printf("First Striffng: ");
printf("%s\n",s);
reverseString(s,5);
printf("Reverse String: ");
printf("%s",s);
return 0;
}
```
:::
:::spoiler {state="open"} 2. edit [賴虓翰's lab2](https://hackmd.io/29AFIJvKQdC2HmfPDoEUAA?view)
```c=
#include <stdio.h>
void reverseString(char* s,int sSize)
{
char tmp;
char* start=s;
char* end=start+sSize-1;
while(end>start)
{
tmp=*end;
*(end--)=*start;
*(start++)=tmp;
}
}
int main()
{
char s[] = "Hello";
printf("First Striffng: ");
printf("%s\n",s);
reverseString(s,5);
printf("Reverse String: ");
printf("%s",s);
return 0;
}
```
3.Havn't found better way for this problem
:::
### Make file
:::spoiler {state="open"} 1. first test `hello.c`
```bash=
$ cd srv32
$ make hello
make[1]: Entering directory '/home/leonard/srv32/sw'
make -C common
make[2]: Entering directory '/home/leonard/srv32/sw/common'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/leonard/srv32/sw/common'
make[2]: Entering directory '/home/leonard/srv32/sw/hello'
riscv-none-embed-gcc -O3 -Wall -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -L../common -o hello.elf hello.c -lc -lm -lgcc -lsys -T ../common/default.ld
riscv-none-embed-objcopy -j .text -O binary hello.elf imem.bin
riscv-none-embed-objcopy -j .data -O binary hello.elf dmem.bin
riscv-none-embed-objcopy -O binary hello.elf memory.bin
riscv-none-embed-objdump -d hello.elf > hello.dis
riscv-none-embed-readelf -a hello.elf > hello.symbol
make[2]: Leaving directory '/home/leonard/srv32/sw/hello'
make[1]: Leaving directory '/home/leonard/srv32/sw'
make[1]: Entering directory '/home/leonard/srv32/sim'
Excuting 113 instructions, 155 cycles, 1.371 CPI
Program terminate
- ../rtl/../testbench/testbench.v:418: Verilog $finish
Simulation statistics
=====================
Simulation time : 0.007 s
Simulation cycles: 166
Simulation speed : 0.0237143 MHz
make[1]: Leaving directory '/home/leonard/srv32/sim'
make[1]: Entering directory '/home/leonard/srv32/tools'
./rvsim --memsize 128 -l trace.log ../sw/hello/hello.elf
PC 0x000000da alignment error
Excuting 113 instructions, 155 cycles, 1.372 CPI
Program terminate
Simulation statistics
=====================
Simulation time : 0.000 s
Simulation cycles: 155
Simulation speed : 2.925 MHz
make[1]: Leaving directory '/home/leonard/srv32/tools'
Compare the trace between RTL and ISS simulator
=== Simulation passed ===
```
:::
Put these two reverse_string.c code into different folder
cd to the folder and `make`
:::spoiler {state="open"} result
```bash=
leonard@leonard:~/srv32$ cd sw/reverse_string
leonard@leonard:~/srv32/sw/reverse_string$ make
riscv-none-embed-gcc -O3 -Wall -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -L../common -o reverse_string.elf reverse_string.c -lc -lm -lgcc -lsys -T ../common/default.ld
reverse_string.c: In function 'main':
reverse_string.c:17:18: warning: unused variable 'tx' [-Wunused-variable]
17 | { volatile char* tx = (volatile char*) 0x40002000;
| ^~
riscv-none-embed-objcopy -j .text -O binary reverse_string.elf imem.bin
riscv-none-embed-objcopy -j .data -O binary reverse_string.elf dmem.bin
riscv-none-embed-objcopy -O binary reverse_string.elf memory.bin
riscv-none-embed-objdump -d reverse_string.elf > reverse_string.dis
riscv-none-embed-readelf -a reverse_string.elf > reverse_string.symbol
```
:::
Than make wave.fst of reverse_sring at ~/srv32
however some error appear
so I test qsort
:::spoiler {state="open"}result:
```bash=
~/srv32$ make qsort
make[1]: Entering directory '/home/leonard/srv32/sw'
make -C common
make[2]: Entering directory '/home/leonard/srv32/sw/common'
make[2]: Nothing to be done for 'all'.
make[2]: Leaving directory '/home/leonard/srv32/sw/common'
make[2]: Entering directory '/home/leonard/srv32/sw/qsort'
riscv-none-embed-gcc -O3 -Wall -march=rv32im -mabi=ilp32 -nostartfiles -nostdlib -L../common -o qsort.elf qsort.c -lc -lm -lgcc -lsys -T ../common/default.ld
riscv-none-embed-objcopy -j .text -O binary qsort.elf imem.bin
riscv-none-embed-objcopy -j .data -O binary qsort.elf dmem.bin
riscv-none-embed-objcopy -O binary qsort.elf memory.bin
riscv-none-embed-objdump -d qsort.elf > qsort.dis
riscv-none-embed-readelf -a qsort.elf > qsort.symbol
make[2]: Leaving directory '/home/leonard/srv32/sw/qsort'
make[1]: Leaving directory '/home/leonard/srv32/sw'
make[1]: Entering directory '/home/leonard/srv32/sim'
Illegal instruction at PC 0x000001e4
Illegal instruction at PC 0x000001e8
Illegal instruction at PC 0x000001ec
Illegal instruction at PC 0x000001f0
Illegal instruction at PC 0x000001f4
Illegal instruction at PC 0x000001f8
Illegal instruction at PC 0x000001fc
Illegal instruction at PC 0x00000214
Illegal instruction at PC 0x00000218
Illegal instruction at PC 0x00000224
Illegal instruction at PC 0x00000228
Illegal instruction at PC 0x0000022c
Illegal instruction at PC 0x00002e90
Illegal instruction at PC 0x00002e94
Illegal instruction at PC 0x00002e98
Illegal instruction at PC 0x00002e9c
Illegal instruction at PC 0x00002eb0
Illegal instruction at PC 0x00002eb4
Illegal instruction at PC 0x00002eb8
Illegal instruction at PC 0x00002ebc
Illegal instruction at PC 0x00002ec0
Illegal instruction at PC 0x00002ec4
Illegal instruction at PC 0x00002ec8
Illegal instruction at PC 0x00002ecc
Illegal instruction at PC 0x00002ed0
Illegal instruction at PC 0x00002edc
Illegal instruction at PC 0x00002ee0
Illegal instruction at PC 0x00002ee4
Illegal instruction at PC 0x00002eec
Illegal instruction at PC 0x00002ef0
Illegal instruction at PC 0x00002ef4
Illegal instruction at PC 0x00002ef8
Illegal instruction at PC 0x00002f00
Illegal instruction at PC 0x00002f0c
Illegal instruction at PC 0x00002f10
Illegal instruction at PC 0x00002f18
Illegal instruction at PC 0x00002f1c
Illegal instruction at PC 0x00002f20
Illegal instruction at PC 0x00002f24
Illegal instruction at PC 0x00002f28
Illegal instruction at PC 0x00002f2c
Illegal instruction at PC 0x00002f34
Illegal instruction at PC 0x00002f38
Illegal instruction at PC 0x00002f3c
Illegal instruction at PC 0x00002f40
Illegal instruction at PC 0x00002f48
Illegal instruction at PC 0x00002f4c
Illegal instruction at PC 0x00002f50
Illegal instruction at PC 0x00002f54
Illegal instruction at PC 0x00002f5c
Illegal instruction at PC 0x00002f60
Illegal instruction at PC 0x00002f64
Illegal instruction at PC 0x00002f68
Illegal instruction at PC 0x00002f6c
Illegal instruction at PC 0x00002f70
Illegal instruction at PC 0x00002f74
Illegal instruction at PC 0x00002f78
Illegal instruction at PC 0x00002f8c
Illegal instruction at PC 0x00002f94
Illegal instruction at PC 0x00002f98
Illegal instruction at PC 0x00002f9c
Illegal instruction at PC 0x00002fa0
Illegal instruction at PC 0x00002fa4
Illegal instruction at PC 0x00002fa8
Illegal instruction at PC 0x000061c0
Illegal instruction at PC 0x000061c4
Illegal instruction at PC 0x000061c8
Illegal instruction at PC 0x000061cc
Illegal instruction at PC 0x000061d0
Illegal instruction at PC 0x000061d4
Illegal instruction at PC 0x000061d8
Illegal instruction at PC 0x000061dc
Illegal instruction at PC 0x000061e0
Illegal instruction at PC 0x000061ec
Illegal instruction at PC 0x000061f0
Illegal instruction at PC 0x000061f4
Illegal instruction at PC 0x000061fc
Illegal instruction at PC 0x00006200
Illegal instruction at PC 0x00006204
Illegal instruction at PC 0x00006208
Illegal instruction at PC 0x0000620c
Illegal instruction at PC 0x00006210
Illegal instruction at PC 0x00006214
Illegal instruction at PC 0x00006218
Illegal instruction at PC 0x00006220
Illegal instruction at PC 0x00006228
Illegal instruction at PC 0x0000622c
Illegal instruction at PC 0x00006230
Illegal instruction at PC 0x00006234
Illegal instruction at PC 0x00006238
Illegal instruction at PC 0x0000623c
Illegal instruction at PC 0x00006240
Illegal instruction at PC 0x00006244
Illegal instruction at PC 0x00006248
Illegal instruction at PC 0x0000624c
Illegal instruction at PC 0x00006250
Illegal instruction at PC 0x00006258
Illegal instruction at PC 0x00006260
Illegal instruction at PC 0x0000626c
Illegal instruction at PC 0x00006270
Illegal instruction at PC 0x00006284
Illegal instruction at PC 0x00006288
Illegal instruction at PC 0x0000628c
Illegal instruction at PC 0x00006290
Illegal instruction at PC 0x000062a0
Illegal instruction at PC 0x000062a4
Illegal instruction at PC 0x000062ac
Illegal instruction at PC 0x000062b0
Illegal instruction at PC 0x000062b4
Illegal instruction at PC 0x000062b8
Illegal instruction at PC 0x000062bc
Illegal instruction at PC 0x000062c0
Illegal instruction at PC 0x000062c4
Illegal instruction at PC 0x000062c8
Illegal instruction at PC 0x000062d4
Illegal instruction at PC 0x000062d8
Illegal instruction at PC 0x000062e4
Illegal instruction at PC 0x000062e8
Illegal instruction at PC 0x000062ec
Illegal instruction at PC 0x000062f0
Illegal instruction at PC 0x000062f4
Illegal instruction at PC 0x000062f8
Illegal instruction at PC 0x000062fc
Illegal instruction at PC 0x00006304
Illegal instruction at PC 0x00006308
Illegal instruction at PC 0x0000630c
Illegal instruction at PC 0x00006320
Illegal instruction at PC 0x00006324
Illegal instruction at PC 0x00006328
Illegal instruction at PC 0x0000632c
Illegal instruction at PC 0x00006330
Illegal instruction at PC 0x00006334
Illegal instruction at PC 0x00006338
Illegal instruction at PC 0x0000633c
Illegal instruction at PC 0x00006340
Illegal instruction at PC 0x00006348
Illegal instruction at PC 0x0000634c
Illegal instruction at PC 0x00006350
Illegal instruction at PC 0x00006354
Illegal instruction at PC 0x00006358
Illegal instruction at PC 0x0000635c
Illegal instruction at PC 0x00006360
Illegal instruction at PC 0x00006364
Illegal instruction at PC 0x00006368
Illegal instruction at PC 0x0000636c
Illegal instruction at PC 0x00006370
Illegal instruction at PC 0x00006374
Illegal instruction at PC 0x00006378
Illegal instruction at PC 0x0000637c
Illegal instruction at PC 0x00006388
Illegal instruction at PC 0x0000638c
Illegal instruction at PC 0x00006390
Illegal instruction at PC 0x00006398
Illegal instruction at PC 0x000063a0
Illegal instruction at PC 0x000063a4
Illegal instruction at PC 0x000063a8
Illegal instruction at PC 0x000063ac
Illegal instruction at PC 0x000063b0
Illegal instruction at PC 0x000063b4
Illegal instruction at PC 0x000063b8
Illegal instruction at PC 0x000063bc
Illegal instruction at PC 0x000063c0
Illegal instruction at PC 0x000063c4
Illegal instruction at PC 0x000063d0
Illegal instruction at PC 0x000063d4
Illegal instruction at PC 0x000063d8
Illegal instruction at PC 0x000063dc
Illegal instruction at PC 0x000063e0
Illegal instruction at PC 0x000063e4
Illegal instruction at PC 0x000063e8
Illegal instruction at PC 0x000063ec
Illegal instruction at PC 0x000063f4
Illegal instruction at PC 0x000063f8
Illegal instruction at PC 0x00006400
Illegal instruction at PC 0x00006404
Illegal instruction at PC 0x0000640c
Illegal instruction at PC 0x00006410
Illegal instruction at PC 0x0000641c
Illegal instruction at PC 0x00006420
Illegal instruction at PC 0x00006428
Illegal instruction at PC 0x0000642c
Illegal instruction at PC 0x00006430
Illegal instruction at PC 0x00006438
Illegal instruction at PC 0x00006440
Illegal instruction at PC 0x00006444
Illegal instruction at PC 0x00006448
Illegal instruction at PC 0x0000644c
Illegal instruction at PC 0x00006450
Illegal instruction at PC 0x00006458
Illegal instruction at PC 0x0000645c
Illegal instruction at PC 0x00006464
Illegal instruction at PC 0x0000646c
Illegal instruction at PC 0x00006470
Illegal instruction at PC 0x00006474
Illegal instruction at PC 0x00006478
Illegal instruction at PC 0x0000647c
Illegal instruction at PC 0x00006484
Illegal instruction at PC 0x00006488
Illegal instruction at PC 0x00006490
Illegal instruction at PC 0x00006494
Illegal instruction at PC 0x00006498
Illegal instruction at PC 0x0000649c
Illegal instruction at PC 0x000064a0
Illegal instruction at PC 0x000064a4
Illegal instruction at PC 0x000064a8
Illegal instruction at PC 0x000064ac
Illegal instruction at PC 0x000064b0
Illegal instruction at PC 0x000064b4
Illegal instruction at PC 0x000064b8
Illegal instruction at PC 0x000064bc
Illegal instruction at PC 0x000064c0
Illegal instruction at PC 0x000064c4
Illegal instruction at PC 0x000064c8
Illegal instruction at PC 0x000064cc
Illegal instruction at PC 0x000064d0
Illegal instruction at PC 0x000064d8
Illegal instruction at PC 0x000064dc
Illegal instruction at PC 0x000064e4
Illegal instruction at PC 0x000064e8
Illegal instruction at PC 0x000064ec
Illegal instruction at PC 0x000064f8
Illegal instruction at PC 0x000064fc
Illegal instruction at PC 0x00006500
Illegal instruction at PC 0x00006504
Illegal instruction at PC 0x00006508
Illegal instruction at PC 0x0000650c
Illegal instruction at PC 0x00006514
Illegal instruction at PC 0x00006518
Illegal instruction at PC 0x0000651c
Illegal instruction at PC 0x00006520
Illegal instruction at PC 0x00006524
Illegal instruction at PC 0x00006528
Illegal instruction at PC 0x0000652c
Illegal instruction at PC 0x00006530
Illegal instruction at PC 0x00006534
Illegal instruction at PC 0x00006538
Illegal instruction at PC 0x0000653c
Illegal instruction at PC 0x00006540
Illegal instruction at PC 0x00006544
Illegal instruction at PC 0x00006548
Illegal instruction at PC 0x00006554
Illegal instruction at PC 0x00006558
Illegal instruction at PC 0x0000655c
Illegal instruction at PC 0x00006560
Illegal instruction at PC 0x00006564
Illegal instruction at PC 0x00006568
Illegal instruction at PC 0x00006570
```
:::
I think it may cause by enviorment
so i will delete all including this virtual env
and than restart again