Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feedback #1

Open
wants to merge 25 commits into
base: feedback
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Day2/1to9_custom.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#include <stdio.h>

extern int load(int x, int y);

int main() {
int result = 0;
int count = 2;
result = load(0x0, count+1);
printf("Sum of number from 1 to %d is %d\n", count, result);
}

72 changes: 71 additions & 1 deletion Day2/README.md
Original file line number Diff line number Diff line change
@@ -1 +1,71 @@
Empty
# Day 2 Content

| File | Description |
| ---- | ----------- |
| [1to9_custom.c](1to9_custom.c) | C program that sums integers from 1 to 9. Calls [load.S](load.S) |
| [hello.c](hello.c) | C program that prints 'Hello World' |
| [hex8tohex32.py](hex8tohex32.py) | Python program that converts byte data to 32-bit word data |
| [load.S](load.S) | Assembly program that performs summation of integers from 1 to N |
| [picorv32.v](picorv32.v) | RISC-V 32-bit core example |
| [riscv.ld](riscv.ld) | RISC-V ld script for example |
| [rv32im.sh](rv32im.sh) | RISC-V simulation bash script. Run this for example |
| [start.S](start.S) | Assembly program for initializing RISC-V example |
| [start.ld](start.ld) | Start ld script for example |
| [syscalls.c](syscalls.c) | C program containing system call definitions for example |
| [testbench.v](testbench.v) | Verilog testbench for RISC-V example |

# Day 1

Day 1 was comprised of the RISC-V ISA and GNU compiler toolchain, which were used in Day 2 labs.

## RISC-V Toolchain

Compile command:

`$ riscv64-unkown-elf-gcc -O<1/fast> -mabi=lp<XLEN> -march=rv<XLEN>i -o <output_program> <input_user_file> [<input_user_file>...]`

Assembly preview command:

`$ riscv64-unkown-elf-objdump -d <output_program>`

Run command:

`$ spike pk <output_program>`

Debug command:

`$ spike -d pk <output_program>`

# Day 2
## Application Binary Interface (ABI)

The Application Binary Interface (ABI), also known as the System Call Interface, is used by the program to access the ISA registers. RISC-V architecture contains 32 registers of width 32/64 if using RV32I/RV64I, respectively:

| Register | ABI Name | Usage |
| -------- | -------- | ----- |
| x0 | zero | Hard-wired zero |
| x1 | ra | Return address |
| x2 | sp | Stack pointer |
| x3 | gp | Global pointer |
| x4 | tp | Thread opinter |
| x5-7 | t0-2 | Temporaries |
| x8 | s0/fp | Saved register/frame pointer |
| x9 | s1 | Saved register |
| x10-11 | a0-1 | Function arguments/return values |
| x12-17 | a2-7 | Function arguments |
| x18-27 | s2-11 | Saved registers |
| x28-31 | t3-6 | Temporaries |

There are 3 types of instructions:
1. **R-type:** operate only on registers<br>
Ex: `add x8, x24, x8`
2. **I-type:** operate on registers and immediate values<br>
Ex: `ld x8, 16(x23)`
3. **S-type:** operate on source registers and store in immediate value<br>
Ex: `sd x8, 8(x23)`

## Day 2 Lab: ABI Function Calls

A simple program for summing numbers 1 to 9 was created; view source [here](1to9_custom.c). The compiled output in assembly looks like this:

![day2_lab_assembly](day2_lab_assembly.png)
Binary file added Day2/day2_lab_assembly.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions Day2/hello.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <stdio.h>

int main() {
printf("Hello World to all first timers!\n");
return 0;
}

34 changes: 34 additions & 0 deletions Day2/hex8tohex32.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

import fileinput
import itertools

ptr = 0
data = []

def write_data():
if len(data) != 0:
print("@%08x" % (ptr >> 2))
while len(data) % 4 != 0:
data.append(0)
for word_bytes in zip(*([iter(data)]*4)):
print("".join(["%02x" % b for b in reversed(word_bytes)]))

for line in fileinput.input():
if line.startswith("@"):
addr = int(line[1:], 16)
if addr > ptr+4:
write_data()
ptr = addr
data = []
while ptr % 4 != 0:
data.append(0)
ptr -= 1
else:
while ptr + len(data) < addr:
data.append(0)
else:
data += [int(tok, 16) for tok in line.split()]

write_data()

14 changes: 14 additions & 0 deletions Day2/load.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.section .text
.global load
.type load, @function

load:
add a4, a0, zero //Initialize sum register a4 with 0x0
add a2, a0, a1 // store count of 10 in register a2. Register a1 is loaded with 0xa (decimal 10) from main program
add a3, a0, zero // initialize intermediate sum register a3 by 0
loop: add a4, a3, a4 // Incremental addition
addi a3, a3, 1 // Increment intermediate register by 1
blt a3, a2, loop // If a3 is less than a2, branch to label named <loop>
add a0, a4, zero // Store final result to register a0 so that it can be read by main program
ret

Loading