owned this note
owned this note
Published
Linked with GitHub
# Lab3: SoftCPU
###### tags: `Computer Architecture 2022`
## Environment Setup
### 1.Prepare GNU toolchain for RISC-V
```
$ cd /tmp
$ wget https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases/download/v12.2.0-1/xpack-riscv-none-elf-gcc-12.2.0-1-linux-x64.tar.gz
$ tar zxvf xpack-riscv-none-elf-gcc-12.2.0-1-linux-x64.tar.gz
$ cp -af xpack-riscv-none-elf-gcc-12.2.0-1 $HOME/riscv-none-elf-gcc
$ cd $HOME/riscv-none-elf-gcc
$ echo "export PATH=`pwd`/bin:$PATH" > setenv
$ cd $HOME
$ source riscv-none-elf-gcc/setenv
$ riscv-none-elf-gcc -v
```
Everything is fine, so we can see the message below:
```
gcc version 12.2.0 (xPack GNU RISC-V Embedded GCC x86_64)
```
### 2.Install the dependant packages
```
$ sudo apt install bison
$ sudo apt install flex
$ sudo apt install autoconf
$ sudo apt install stkwave
$ sudo apt install build-essential
$ sudo apt install lcov ccache libsystemc-dev
```
### 3.Get the source
```
git clone https://github.com/sysprog21/srv32
```
First, I try the hello to validate whether I build up the environment
```
$ make hello
Simulation statistics
=====================
Simulation time : 0.026 s
Simulation cycles: 1568
Simulation speed : 0.0603077 MHz
make[1]: Leaving directory '/home/ca/srv32/sim'
make[1]: Entering directory '/home/ca/srv32/tools'
gcc -c -o rvsim.o rvsim.c -O3 -g -Wall
gcc -c -o decompress.o decompress.c -O3 -g -Wall
gcc -c -o syscall.o syscall.c -O3 -g -Wall
gcc -c -o elfread.o elfread.c -O3 -g -Wall
gcc -c -o getch.o getch.c -O3 -g -Wall
gcc -O3 -g -Wall -o rvsim rvsim.o decompress.o syscall.o elfread.o getch.o
./rvsim --memsize 128 -l trace.log ../sw/hello/hello.elf
hello world!
Excuting 1155 instructions, 1557 cycles, 1.348 CPI
Program terminate
Simulation statistics
=====================
Simulation time : 0.000 s
Simulation cycles: 1557
Simulation speed : 3.414 MHz
make[1]: Leaving directory '/home/ca/srv32/tools'
Compare the trace between RTL and ISS simulator
=== Simulation passed ===
```
So next I try to use my Assignment3 code.
## Leetcode
### [969. Pancake Sorting](https://leetcode.com/problems/pancake-sorting/)
### C code
Below is my C code, and we need to create a new folder under ../srv32/sw/hw3 and put the c file into there.
``` C
#include <stdio.h>
int MaxIndex(int *arr, int limit)
{
int max = 0, index;
for (int i = 0; i < limit; i++)
{
if (arr[i] > max)
{
max = arr[i];
index = i;
}
}
return index;
}
void reverse(int *arr, int index)
{
int temp;
for (int i = 0; i < index / 2; i++)
{
temp = arr[i];
arr[i] = arr[index - i - 1];
arr[index - i - 1] = temp;
}
}
void pancakeSort(int *arr, int arrSize)
{
int index;
for (int i = arrSize; i > 0; i--)
{
index = MaxIndex(arr, i);
reverse(arr, index + 1);
printf("%d ", index + 1);
reverse(arr, i);
printf("%d ", i);
}
}
int main()
{
int arr[4] = {3, 2, 4, 1};
int arrSize = 4;
pancakeSort(arr, arrSize);
}
```
### Modify Makefile
Modify the Makefile by example of sw/hello to let it fit hw3.c
```
include ../common/Makefile.common
EXE = .elf
SRC = hw3.c
SSRC = hw3.s
CFLAGS += -L../common
LDFLAGS += -T ../common/default.ld
TARGET = hw3
OUTPUT = $(TARGET)$(EXE)
.PHONY: all clean
all: $(TARGET)
$(TARGET): $(SRC)
$(CC) $(CFLAGS) -o $(OUTPUT) $(SRC) $(LDFLAGS)
$(OBJCOPY) -j .text -O binary $(OUTPUT) imem.bin
$(OBJCOPY) -j .data -O binary $(OUTPUT) dmem.bin
$(OBJCOPY) -O binary $(OUTPUT) memory.bin
$(OBJDUMP) -d $(OUTPUT) > $(TARGET).dis
$(READELF) -a $(OUTPUT) > $(TARGET).symbol
clean:
$(RM) *.o $(OUTPUT) $(TARGET).dis $(TARGET).symbol [id]mem.bin memory.bin
```
And then
```
$ make hw3
```
RTL Simulation result
```
Excuting 6819 instructions, 9085 cycles, 1.332 CPI
Program terminate
- ../rtl/../testbench/testbench.v:434: Verilog $finish
Simulation statistics
=====================
Simulation time : 0.032 s
Simulation cycles: 9096
Simulation speed : 0.28425 MHz
```
ISS result
```
Excuting 6819 instructions, 9085 cycles, 1.332 CPI
Program terminate
Simulation statistics
=====================
Simulation time : 0.002 s
Simulation cycles: 9085
Simulation speed : 5.618 MHz
```