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

Lab0/1 #42

Open
wants to merge 1 commit into
base: 311553047
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
25 changes: 25 additions & 0 deletions Lab1/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
SRCS = $(wildcard *.c)
OBJS = $(SRCS:.c=.o)
CCFLAGS = -Wall -O2 -ffreestanding -nostdinc -nostdlib -nostartfiles
CC = aarch64-linux-gnu-gcc
LINKER = aarch64-linux-gnu-ld
OBJ_CPY = aarch64-linux-gnu-objcopy

all: clean kernel8.img

start.o: start.S
$(CC) $(CCFLAGS) -c start.S -o start.o

%.o: %.c
$(CC) $(CCFLAGS) -c $< -o $@

kernel8.img: start.o $(OBJS)
$(LINKER) start.o $(OBJS) -T link.ld -o kernel8.elf
$(OBJ_CPY) -O binary kernel8.elf kernel8.img

clean:
rm kernel8.elf kernel8.img *.o >/dev/null 2>/dev/null || true

run:
qemu-system-aarch64 -M raspi3b -kernel kernel8.img -serial null -serial stdio

22 changes: 22 additions & 0 deletions Lab1/READ.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# OSC2023

| Github Account | Student ID | Name |
|----------------|------------|---------------|
| linlianen | 311553047 | Lin Lian En |

## Requirements

* a cross-compiler for aarch64
* (optional) qemu-system-arm

## Build

```
make kernel.img
```

## Test With QEMU

```
qemu-system-aarch64 -M raspi3b -kernel kernel.img -initrd initramfs.cpio -serial null -serial stdio -dtb bcm2710-rpi-3-b-plus.dtb
```
62 changes: 62 additions & 0 deletions Lab1/command.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#include "uart.h"
#include "string.h"

void input_buffer_overflow_message ( char cmd[] )
{
uart_puts("Follow command: \"");
uart_puts(cmd);
uart_puts("\"... is too long to process.\n");

uart_puts("The maximum length of input is 64.");
}

void command_help ()
{
uart_puts("\n");
uart_puts("Valid Command:\n");
uart_puts("\thelp:\t\tprint this help.\n");
uart_puts("\thello:\t\tprint \"Hello World!\".\n");
uart_puts("\ttimestamp:\tget current timestamp.\n");
uart_puts("\n");
}

void command_hello ()
{
uart_puts("Hello World!\n");
}

void command_timestamp ()
{
unsigned long int cnt_freq, cnt_tpct;
char str[20];

asm volatile(
"mrs %0, cntfrq_el0 \n\t"
"mrs %1, cntpct_el0 \n\t"
: "=r" (cnt_freq), "=r" (cnt_tpct)
:
);

ftoa( ((float)cnt_tpct) / cnt_freq, str, 6);

uart_send('[');
uart_puts(str);
uart_puts("]\n");
}

void command_not_found (char * s)
{
uart_puts("Err: command ");
uart_puts(s);
uart_puts(" not found, try <help>\n");
}

void command_reboot ()
{
uart_puts("Start Rebooting...\n");

*PM_WDOG = PM_PASSWORD | 0x20;
*PM_RSTC = PM_PASSWORD | 100;

while(1);
}
12 changes: 12 additions & 0 deletions Lab1/command.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#ifndef COMMAND_H
#define COMMAND_H

void input_buffer_overflow_message ( char [] );

void command_help ();
void command_hello ();
void command_timestamp ();
void command_not_found ( char * );
void command_reboot ();

#endif
Binary file added Lab1/command.o
Binary file not shown.
50 changes: 50 additions & 0 deletions Lab1/gpio.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2018 bzt (bztsrc@github)
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without
* restriction, including without limitation the rights to use, copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/

#ifndef GPIO_H
#define GPIO_H

#define MMIO_BASE 0x3F000000

#define GPFSEL0 ((volatile unsigned int*)(MMIO_BASE+0x00200000))
#define GPFSEL1 ((volatile unsigned int*)(MMIO_BASE+0x00200004))
#define GPFSEL2 ((volatile unsigned int*)(MMIO_BASE+0x00200008))
#define GPFSEL3 ((volatile unsigned int*)(MMIO_BASE+0x0020000C))
#define GPFSEL4 ((volatile unsigned int*)(MMIO_BASE+0x00200010))
#define GPFSEL5 ((volatile unsigned int*)(MMIO_BASE+0x00200014))
#define GPSET0 ((volatile unsigned int*)(MMIO_BASE+0x0020001C))
#define GPSET1 ((volatile unsigned int*)(MMIO_BASE+0x00200020))
#define GPCLR0 ((volatile unsigned int*)(MMIO_BASE+0x00200028))
#define GPLEV0 ((volatile unsigned int*)(MMIO_BASE+0x00200034))
#define GPLEV1 ((volatile unsigned int*)(MMIO_BASE+0x00200038))
#define GPEDS0 ((volatile unsigned int*)(MMIO_BASE+0x00200040))
#define GPEDS1 ((volatile unsigned int*)(MMIO_BASE+0x00200044))
#define GPHEN0 ((volatile unsigned int*)(MMIO_BASE+0x00200064))
#define GPHEN1 ((volatile unsigned int*)(MMIO_BASE+0x00200068))
#define GPPUD ((volatile unsigned int*)(MMIO_BASE+0x00200094))
#define GPPUDCLK0 ((volatile unsigned int*)(MMIO_BASE+0x00200098))
#define GPPUDCLK1 ((volatile unsigned int*)(MMIO_BASE+0x0020009C))

#endif
Binary file added Lab1/kernel8.elf
Binary file not shown.
Binary file added Lab1/kernel8.img
Binary file not shown.
19 changes: 19 additions & 0 deletions Lab1/link.ld
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
SECTIONS
{
. = 0x80000;
.text : { KEEP(*(.text.boot)) *(.text .text.* .gnu.linkonce.t*) }
.rodata : { *(.rodata .rodata.* .gnu.linkonce.r*) }
PROVIDE(_data = .);
.data : { *(.data .data.* .gnu.linkonce.d*) }
.bss (NOLOAD) : {
. = ALIGN(16);
__bss_start = .;
*(.bss .bss.*)
*(COMMON)
__bss_end = .;
}
_end = .;

/DISCARD/ : { *(.comment) *(.gnu*) *(.note*) *(.eh_frame*) }
}
__bss_size = (__bss_end - __bss_start)>>3;
16 changes: 16 additions & 0 deletions Lab1/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include "uart.h"
#include "shell.h"

int main()
{
// set up serial console
uart_init();

// say hello
uart_puts("Hello World!\n");

// start shell
shell_start();

return 0;
}
Binary file added Lab1/main.o
Binary file not shown.
11 changes: 11 additions & 0 deletions Lab1/math.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@


int pow(int base, int exponent)
{
int result = 1;
for ( ; exponent > 0; exponent--)
{
result = result * base;
}
return result;
}
7 changes: 7 additions & 0 deletions Lab1/math.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

#ifndef MATH_H
#define MATH_H

int pow(int base, int exponent);

#endif
Binary file added Lab1/math.o
Binary file not shown.
112 changes: 112 additions & 0 deletions Lab1/shell.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@



#include "shell.h"
#include "string.h"
#include "command.h"
#include "uart.h"

void shell_start ()
{
int buffer_counter = 0;
char input_char;
char buffer[MAX_BUFFER_LEN];
enum SPECIAL_CHARACTER input_parse;

strset (buffer, 0, MAX_BUFFER_LEN);

// new line head
uart_puts("# ");

// read input
while(1)
{
input_char = uart_getc();



input_parse = parse ( input_char );

command_controller ( input_parse, input_char, buffer, &buffer_counter);
}
}

enum SPECIAL_CHARACTER parse ( char c )
{
if ( !(c < 128 && c >= 0) )
return UNKNOWN;

if ( c == 127 )
return BACK_SPACE;
else if ( c == LINE_FEED || c == CARRIAGE_RETURN )
return NEW_LINE;
else
return REGULAR_INPUT;
}

void command_controller ( enum SPECIAL_CHARACTER input_parse, char c, char buffer[], int * counter )
{


if ( input_parse == UNKNOWN )
return;

// Special key
if ( input_parse == BACK_SPACE )
{

if ( (*counter) > 0 )
{
(*counter)--;
buffer[(*counter)] = '\0';

// int* end = counter;
for (int i = 0; i < (counter) ; i++) {
buffer[i] = buffer[i+1];
}

uart_puts(buffer);

}





}
else if ( input_parse == NEW_LINE )
{
uart_send(c);

if ( (*counter) == MAX_BUFFER_LEN )
{
input_buffer_overflow_message(buffer);
}
else
{
buffer[(*counter)] = '\0';

if ( !strcmp(buffer, "help" ) ) command_help();
else if ( !strcmp(buffer, "hello" ) ) command_hello();
else if ( !strcmp(buffer, "timestamp" ) ) command_timestamp();
else if ( !strcmp(buffer, "reboot" ) ) command_reboot();
else command_not_found(buffer);
}

(*counter) = 0;
strset (buffer, 0, MAX_BUFFER_LEN);

// new line head;
uart_puts("# ");
}
else if ( input_parse == REGULAR_INPUT )
{
uart_send(c);

if ( *counter < MAX_BUFFER_LEN)
{
buffer[*counter] = c;
(*counter) ++;
}
}
}
24 changes: 24 additions & 0 deletions Lab1/shell.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef SHELL_H
#define SHELL_H

#define MAX_BUFFER_LEN 128

enum SPECIAL_CHARACTER
{
BACK_SPACE = 8,
LINE_FEED = 10,
CARRIAGE_RETURN = 13,


REGULAR_INPUT = 1000,
NEW_LINE = 1001,

UNKNOWN = -1,

};

void shell_start () ;
enum SPECIAL_CHARACTER parse ( char );
void command_controller ( enum SPECIAL_CHARACTER, char c, char [], int *);

#endif
Binary file added Lab1/shell.o
Binary file not shown.
Loading