Hasham Initial Report
🧩 Syntax:
Assignment Report
Introduction
In this report, we present our implementation of the three tasks assigned to optimize and improve the Retro Pocket handheld game console platform. We will discuss the implementation of the following:
1. An assembly subroutine for generating random seed values and a Pseudo Random Number Generator (PRNG).
2. An assembly subroutine for displaying an LED animation during console startup.
3. C functions for manipulating zero-terminated strings.
Task 1
Part 1: Wait_start subroutine
We implemented the wait_start subroutine using the provided timer mapped to memory address 0xF1. The subroutine checks if any key has been pressed by monitoring the IP_Keybd_7bit input buttons at memory address 0xF0. The timer value is returned as a byte in r0. The following assembly code demonstrates the implementation of the wait_start subroutine:
```assembly
wait_start:
ld r0, 0xF1 ; Load timer value into r0
ld r1, 0xF0 ; Load keyboard input value into r1
beq r1, wait_start ; Loop until a key is pressed
rts
```
Part 2: PRNG subroutine
The PRNG subroutine, named prng_next, uses the seed value generated by the wait_start subroutine. The PRNG implementation is based on the Linear Congruential Generator (LCG) algorithm, which is simple and efficient. The LCG formula is:
X_(n+1) = (a * X_n + c) mod m
Here, we use a = 1664525, c = 1013904223, and m = 2^24 as constants, which provide a good balance between quality and performance for 8-bit random numbers.
```assembly
prng_next:
; Load seed value into r3
ld r3, prng_data_seed
; Calculate new random number
ld r4, 1664525
mul r3, r4
ld r4, 1013904223
add r3, r4
ld r4, 0x1000000
mod r3, r4
; Store new seed value
st r3, prng_data_seed
; Get random number between r0 and r1
sub r1, r0
mod r3, r1
add r3, r0
mov r2, r3
rts
prng_data:
seed: ds 1
```
Task 2: LED Animation
We implemented the LED animation subroutine using the timer and OP_LEDs_8x1 (mapped to 0xF2). The subroutine animates an LED sweeping back and forth smoothly. The following assembly code demonstrates the implementation of the LED animation subroutine:
```assembly
led_animation:
; Load timer value into r0
ld r0, 0xF1
; Calculate LED position
mod r0, 14
cmp r0, 7
bgt r0, reverse
forward:
st r0, 0xF2
jmp exit
reverse:
sub r0, 14
neg r0
st r0, 0xF2
exit:
rts
```
Task 3: String Manipulation Functions
We implemented three C functions for manipulating zero-terminated strings:
1. replace_char
2. count_char
3. remove_char
Here is the revised implementation of the string manipulation functions:
```c
#include <string.h>
void replace_char(char *s, char old_char, char new_char) {
for (int i = 0; s[i]; i++) {
if (s[i] == old_char) {
s[i] = new_char;
}
}
}
int count_char(const char *s, char target_char) {
int count = 0;
for (int i = 0; s[i]; i++) {
if (s[i] == target_char) {
count++;
}
}
return count;
}
void remove_char(char *s, char target_char) {
int write_index = 0;
for (int read_index = 0; s[read_index]; read_index++) {
if (s[read_index] != target_char) {
s[write_index++] = s[read_index];
}
}
s[write_index] = '\0';
}
```
These functions provide basic string manipulation capabilities as requested:
1. The `replace_char` function takes a string `s`, an old character `old_char`, and a new character `new_char`. It iterates through the string and replaces all instances of `old_char` with `new_char`.
2. The `count_char` function takes a string `s` and a target character `target_char`. It iterates through the string and counts the number of occurrences of `target_char`, returning the count.
3. The `remove_char` function takes a string `s` and a target character `target_char`. It iterates through the string and removes all instances of `target_char` by shifting the remaining characters to the left and updating the string's null terminator.
These functions can be utilized in various applications, such as text processing, input validation, or simple string manipulation tasks, enhancing the functionality of the Retro Pocket handheld game console platform.