Skip to content

Commit

Permalink
Redone the labs
Browse files Browse the repository at this point in the history
  • Loading branch information
Adriano-7 committed May 27, 2023
1 parent cbda3fb commit 1d30e57
Show file tree
Hide file tree
Showing 54 changed files with 1,588 additions and 1,424 deletions.
53 changes: 0 additions & 53 deletions lab2/i8254.h

This file was deleted.

15 changes: 8 additions & 7 deletions lab2/lab2.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,21 @@ int main(int argc, char *argv[]) {
int(timer_test_read_config)(uint8_t timer, enum timer_status_field field) {
uint8_t st;
if(timer_get_conf(timer, &st)){
printf("timer_test_read_config() -> Error geting the timer configuration\n");
printf("Error retrieving the timer configuration\n");
return 1;
}

if(timer_display_conf (timer, st, field)){
printf("timer_test_read_config() -> Error while displaying the timer's configuration");
if(timer_display_conf(timer, st, field)){
printf("Error displaying the timer configuration\n");
return 1;
}

return 0;
}

int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
if(timer_set_frequency(timer, freq)){
printf("timer_test_time_base() -> Error setting timer %d frequency", timer);
printf("Error setting timer %d frequency", timer);
return 1;
}
return 0;
Expand All @@ -58,7 +59,7 @@ int(timer_test_int)(uint8_t time) {
uint8_t timer_bit_no;

if(timer_subscribe_int(&timer_bit_no)){
printf("timer_test_int() -> Error subscribing timer interrupts\n");
printf("Error subscribing timer interrupts\n");
return 1;
}

Expand All @@ -72,7 +73,7 @@ int(timer_test_int)(uint8_t time) {
case HARDWARE:
if (msg.m_notify.interrupts & BIT(timer_bit_no)) {
timer_int_handler();
if(timer_counter % 60==0){
if(timer_counter%60==0){
time--;
timer_print_elapsed_time();
}
Expand All @@ -85,7 +86,7 @@ int(timer_test_int)(uint8_t time) {
}

if(timer_unsubscribe_int()){
printf("timer_test_int() -> Error unsubscribing timer interrupts\n");
printf("Error unsubscribing timer interrupts\n");
return 1;
}

Expand Down
17 changes: 17 additions & 0 deletions lab2/macros.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*Timer*/
#define TIMER_0_IRQ 0
#define TIMER_CLOCK_FREQ 1193182

#define TIMER_REG(timer) 0x40+timer
#define TIMER_CNTRL_REG 0x43

#define TIMER_RBK (BIT(7) | BIT(6))
#define TIMER_RBK_NOT_COUNT BIT(5)
#define TIMER_RBK_NOT_STATUS BIT(4)
#define TIMER_RBK_SEL(timer) BIT((timer) + 1)

#define TIMER_CNTRL_WORD_SEL(timer) (timer << 6)
#define TIMER_CNTRL_INIT_LSB_MSB (BIT(4) | BIT(5))

#define TIMER_STATUS_ACCESS (BIT(4) | BIT(5))
#define TIMER_STATUS_OP_MODE (BIT(3) | BIT(2) | BIT(1))
73 changes: 32 additions & 41 deletions lab2/timer.c
Original file line number Diff line number Diff line change
@@ -1,64 +1,59 @@
#include <lcom/lcf.h>
#include <lcom/timer.h>

#include <stdint.h>

#include "i8254.h"
#include "timer.h"

int timer_hook_id = 0;
int timer_counter = 0;

int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
uint16_t initialValue = TIMER_CLOCK_FREQ / freq;
uint8_t lsbVal, msbVal;

if(util_get_LSB(initialValue, &lsbVal)){printf("Error retrieving the lsb of the value\n");return 1;}
if(util_get_MSB(initialValue, &msbVal)){printf("Error retrieving the msb of the value\n");return 1;}


uint8_t st;
if(timer_get_conf(timer, &st)){
printf("timer_set_frequency() -> Error getting conf\n");
printf("Error retrieving the status\n");
return 1;
}

uint8_t control_word = (TIMER_SEL(timer) | TIMER_LSB_MSB | (st & 0x0F));

if(sys_outb(TIMER_CTRL, control_word)){
printf("timer_set_frequency() -> Error writing control word\n");
uint8_t controlWord = (TIMER_CNTRL_WORD_SEL(timer) | TIMER_CNTRL_INIT_LSB_MSB | (st & 0x0F));
if(sys_outb(TIMER_CNTRL_REG, controlWord)){
printf("Error writing the control word\n");
return 1;
}

uint16_t val = 1193182/freq;

uint8_t val_lsb;
util_get_LSB(val, &val_lsb);
if(sys_outb(TIMER_REG(timer), val_lsb)){
printf("timer_set_frequency() -> Error writing lsb count value\n");
if(sys_outb(TIMER_REG(timer), lsbVal)){
printf("Error writing the lsb value\n");
return 1;
}

uint8_t val_msb;
util_get_MSB(val, &val_msb);
if(sys_outb(TIMER_REG(timer), val_msb)){
printf("timer_set_frequency() -> Error writing msb count value\n");
if(sys_outb(TIMER_REG(timer), msbVal)){
printf("Error writing the msb value\n");
return 1;
}

return 0;
}

int (timer_subscribe_int)(uint8_t *bit_no) {
if(bit_no == NULL){
printf("timer_subscribe_int() -> bit_no is nullptr\n");
if(bit_no==NULL){printf("Bit_no is a null pointer\n"); return 1;}
*bit_no = timer_hook_id;

if(sys_irqsetpolicy(TIMER_0_IRQ, IRQ_REENABLE, &timer_hook_id)){
printf("Error subscribing the timer\n");
return 1;
}

if(sys_irqsetpolicy(TIMER0_IRQ, IRQ_REENABLE, &timer_hook_id)){
printf("timer_subscribe_int() -> Error setting policy\n");
return 1;
}
return 0;
}

int (timer_unsubscribe_int)() {
if(sys_irqrmpolicy(&timer_hook_id)){
printf("timer_subscribe_int() -> Error removing policy\n");
printf("Error unsubscribing the timer\n");
return 1;
}

return 0;
}

Expand All @@ -67,43 +62,39 @@ void (timer_int_handler)() {
}

int (timer_get_conf)(uint8_t timer, uint8_t *st) {
if(st==NULL){
printf("timer_get_conf() -> st is nullptr\n");
return 1;
}
if(st==NULL){printf("Status is a null pointer\n"); return 1;}

uint8_t rbk = (TIMER_RB_CMD) | TIMER_RB_COUNT_ | TIMER_RB_SEL(timer);
if(sys_outb(TIMER_CTRL, rbk)){
printf("timer_get_conf() -> Error writing rbk\n");
uint8_t readBackCommand = (TIMER_RBK | TIMER_RBK_NOT_COUNT | TIMER_RBK_SEL(timer));
if(sys_outb(TIMER_CNTRL_REG, readBackCommand)){
printf("Error trying to write the rbk\n");
return 1;
}

if(util_sys_inb(TIMER_REG(timer), st)){
printf("timer_get_conf() -> Error reading status\n");
printf("Error reading the timer's register\n");
return 1;
}

return 0;
}

int (timer_display_conf)(uint8_t timer, uint8_t st,
enum timer_status_field field) {

int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field) {
union timer_status_field_val conf;
switch(field){
case tsf_all:
conf.byte = st;
break;

case tsf_initial:
st = (st & (BIT(5) | BIT(4))) >> 4;
st = (st & TIMER_STATUS_ACCESS) >> 4;
if(st == 1) conf.in_mode = LSB_only;
else if(st == 2) conf.in_mode = MSB_only;
else if (st == 3) conf.in_mode = MSB_after_LSB;
else conf.in_mode = INVAL_val;
break;

case tsf_mode:
st = (st & (BIT(3) | BIT(2) | BIT(1))) >> 1;
st = (st & TIMER_STATUS_OP_MODE) >> 1;
if(st == 6) conf.count_mode = 2;
else if(st == 7) conf.count_mode = 3;
else conf.count_mode = st;
Expand Down
22 changes: 22 additions & 0 deletions lab2/timer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#ifndef _LCOM_TIMER_H_
#define _LCOM_TIMER_H_

#include <lcom/lcf.h>
#include <stdint.h>
#include "macros.h"

int (timer_set_frequency)(uint8_t timer, uint32_t freq);

int (timer_subscribe_int)(uint8_t *bit_no);

int (timer_unsubscribe_int)();

void (timer_int_handler)();

int (timer_get_conf)(uint8_t timer, uint8_t *st);

int (timer_display_conf)(uint8_t timer, uint8_t st, enum timer_status_field field);


#endif

16 changes: 7 additions & 9 deletions lab2/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,23 @@
#include <stdint.h>

int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
if(lsb==NULL){printf("util_get_LSB() -> lsb is nullptr\n"); return 1;}
if(lsb==NULL){printf("Lsb is a null pointer\n"); return 1;}
*lsb = (uint8_t) val;
return 0;
}

int(util_get_MSB)(uint16_t val, uint8_t *msb) {
if(msb==NULL){printf("util_get_MSB() -> msb is nullptr\n"); return 1;}
if(msb==NULL){printf("Msb is a null pointer\n"); return 1;}
*msb = (uint8_t) (val >> 8);
return 0;
}

int (util_sys_inb)(int port, uint8_t *value) {
if(value==NULL){printf("util_sys_inb() -> value is nullptr\n"); return 1;}
uint32_t val_32;
if(value==NULL){printf("Value is a null pointer\n"); return 1;}
uint32_t val;
if(sys_inb(port, &val)){printf("Error reading from the port\n"); return 1;}

if(sys_inb(port, &val_32)){
printf("util_sys_inb() -> Error regarding sys_inb()\n");
return 1;
}
*value = (uint8_t) val_32;
util_get_LSB(val, value);

return 0;
}
Empty file removed lab3/.gdbinit
Empty file.
2 changes: 1 addition & 1 deletion lab3/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
PROG=lab3

SRCS=lab3.c kbc.c keyboard.c utils.c timer.c
SRCS=lab3.c keyboard.c utils.c timer.c

CFLAGS += -pedantic -D lab3

Expand Down
27 changes: 0 additions & 27 deletions lab3/i8042.h

This file was deleted.

Loading

0 comments on commit 1d30e57

Please sign in to comment.