[AC2] Exercícios adicionais (ex1-4)

Signed-off-by: TiagoRG <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-04-07 19:48:46 +01:00
parent b713e978cf
commit 2f29d3772c
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
6 changed files with 281 additions and 0 deletions

View File

@ -0,0 +1,19 @@
#include <detpic32.h>
int reverse_byte(int byte) {
byte = (byte & 0xF0) >> 4 | (byte & 0x0F) << 4;
byte = (byte & 0xCC) >> 2 | (byte & 0x33) << 2;
byte = (byte & 0xAA) >> 1 | (byte & 0x55) << 1;
return byte;
}
int main() {
TRISB |= 0x000F;
TRISE &= 0xFF00;
while (1) {
int sw = PORTB & 0x000F;
int reverse = reverse_byte(sw);
LATE = (LATE & 0xFF00) | sw | reverse;
}
}

View File

@ -0,0 +1,66 @@
.equ ADDR_BASE, 0xBF88
.equ TRISB, 0x6040
.equ PORTB, 0x6050
.equ TRISE, 0x6100
.equ LATE, 0x6120
.data
.text
.globl main
main:
lui $t7, ADDR_BASE
lw $t0, TRISB($t7)
ori $t0, $t0, 0x000F
sw $t0, TRISB($t7)
lw $t0, TRISE($t7)
andi $t0, $t0, 0xFF00
sw $t0, TRISE($t7)
loop:
lw $t0, PORTB($t7)
andi $t0, $t0, 0x000F
move $a0, $t0
addi $sp, $sp, -8
sw $ra, 0($sp)
sw $t0, 4($sp)
jal reverse_byte
lw $ra, 0($sp)
lw $t0, 4($sp)
addi $sp, $sp, 8
or $t0, $t0, $v0
lw $t1, LATE($t7)
andi $t1, $t1, 0xFF00
or $t1, $t1, $t0
sw $t1, LATE($t7)
j loop
jr $ra
reverse_byte:
move $v0, $a0
andi $t0, $v0, 0xF0
srl $t0, $t0, 4
andi $t1, $v0, 0x0F
sll $t1, $t1, 4
or $v0, $t0, $t1
andi $t0, $v0, 0xCC
srl $t0, $t0, 2
andi $t1, $v0, 0x33
sll $t1, $t1, 2
or $v0, $t0, $t1
andi $t0, $v0, 0xAA
srl $t0, $t0, 1
andi $t1, $v0, 0x55
sll $t1, $t1, 1
or $v0, $t0, $t1
jr $ra

View File

@ -0,0 +1,25 @@
#include <detpic32.h>
void delay(unsigned int ms) {
resetCoreTimer();
while (readCoreTimer() < ms * 20000);
}
int main() {
TRISE &= 0xFFF0;
LATE &= 0xFFF0;
while (1) {
char key = inkey();
if ('0' <= key && key <= '3') {
LATE = (LATE & 0xFFF0) | 0x0001 << (key - '0');
} else if (key != '\0') {
LATE = (LATE & 0xFFF0) | 0x000F;
delay(1000);
LATE &= 0xFFF0;
}
}
return 0;
}

View File

@ -0,0 +1,102 @@
.equ INKEY, 1
.equ GETCHAR, 2
.equ PUTCHAR, 3
.equ READ_INT, 4
.equ READ_INT10, 5
.equ PRINT_INT, 6
.equ PRINT_INT10, 7
.equ PRINT_STR, 8
.equ READ_STR, 9
.equ EXIT, 10
.equ READ_CORE_TIMER, 11
.equ RESET_CORE_TIMER, 12
.equ ADDR_BASE, 0xBF88
.equ TRISE, 0x6100
.equ LATE, 0x6120
.data
.text
.globl main
main:
lui $t7, ADDR_BASE
lw $t0, TRISE($t7)
andi $t0, $t0, 0xFFF0
sw $t0, TRISE($t7)
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
sw $t0, LATE($t7)
loop:
li $v0, INKEY
syscall
move $t0, $v0
beq $t0, 0, loop
beq $t0, '0', sw_0
beq $t0, '1', sw_1
beq $t0, '2', sw_2
beq $t0, '3', sw_3
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
ori $t0, $t0, 0x000F
sw $t0, LATE($t7)
li $a0, 1000
move $s0, $ra
jal delay
move $ra, $s0
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
sw $t0, LATE($t7)
j end
sw_0:
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
ori $t0, $t0, 0x0001
sw $t0, LATE($t7)
j end
sw_1:
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
ori $t0, $t0, 0x0002
sw $t0, LATE($t7)
j end
sw_2:
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
ori $t0, $t0, 0x0004
sw $t0, LATE($t7)
j end
sw_3:
lw $t0, LATE($t7)
andi $t0, $t0, 0xFFF0
ori $t0, $t0, 0x0008
sw $t0, LATE($t7)
j end
end:
j loop
jr $ra
delay:
mul $t0, $a0, 20000
li $v0, RESET_CORE_TIMER
syscall
delay_loop:
li $v0, READ_CORE_TIMER
syscall
move $t1, $v0
blt $t1, $t0, delay_loop
jr $ra

View File

@ -0,0 +1,69 @@
#include <detpic32.h>
const unsigned int dis7Scodes[] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D,
0xFD, 0x07, 0x7F, 0x6F, 0x77, 0xFC,
0x39, 0x5E, 0xF9, 0xF1};
void delay(unsigned int ms) {
resetCoreTimer();
while (readCoreTimer() < ms * 20000);
}
typedef enum { HIGH, LOW } flag;
void clearDisplays() {
LATD = (LATD & 0xFF9F) | 0x0040;
LATB &= 0x80FF;
LATD = (LATD & 0xFF9F) | 0x0020;
LATB &= 0x80FF;
}
void send2displays(unsigned char value) {
static flag f = LOW;
char high = dis7Scodes[value >> 4];
char low = dis7Scodes[value & 0x0F];
if (f == HIGH) {
LATD = (LATD & 0xFF9F) | 0x0040;
LATB = (LATB & 0x80FF) | high << 8;
f = LOW;
} else {
LATD = (LATD & 0xFF9F) | 0x0020;
LATB = (LATB & 0x80FF) | low << 8;
f = HIGH;
}
}
int main() {
TRISE &= 0xFFF0;
TRISD &= 0xFF9F;
TRISB &= 0x80FF;
LATE &= 0xFFF0;
clearDisplays();
char lastKey;
while (1) {
char key = inkey();
if ('0' <= key && key <= '3') {
LATE = (LATE & 0xFFF0) | 0x0001 << (key - '0');
lastKey = key;
} else if (key != '\0') {
LATE = (LATE & 0xFFF0) | 0x000F;
resetCoreTimer();
while (readCoreTimer() < 20000000)
send2displays(0xFF);
LATE &= 0xFFF0;
clearDisplays();
lastKey = 0xFF;
}
if ('0' <= lastKey && lastKey <= '3') {
send2displays(lastKey - '0');
delay(10);
}
}
return 0;
}