Project

General

Profile

Revision 4

First

View differences:

lab3/.vscode/c_cpp_properties.json
1
{
2
  "env": {
3
    "minix.include": "${workspaceFolder}/../.minix-src/include"
4
  },
5
  "configurations": [{
6
    "name": "Minix",
7
    "compilerPath": "",
8
    "cStandard": "c11",
9
    "defines": ["__minix", "_MINIX ", "_NETBSD_SOURCE", "__i386__"],
10
    "includePath": ["${workspaceFolder}", "${env:minix.include}"],
11
    "browse": {
12
      "path": ["${workspaceFolder}", "${env:minix.include}"],
13
      "limitSymbolsToIncludedHeaders": false,
14
      "databaseFilename": ""
15
    }
16
  }],
17
  "version": 4
18
}
0 19

  
lab3/.vscode/extensions.json
1
{
2
	// See http://go.microsoft.com/fwlink/?LinkId=827846 to learn about workspace recommendations.
3
	// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
4
	// List of extensions which should be recommended for users of this workspace.
5
	"recommendations": [
6
		"austin.code-gnu-global",
7
		"ms-vscode.cpptools",
8
		"xaver.clang-format"
9
	],
10
	// List of extensions recommended by VS Code that should not be recommended for users of this workspace.
11
	"unwantedRecommendations": []
12
}
0 13

  
lab3/.vscode/settings.json
1
{
2
  "git.ignoreLimitWarning": true,
3
  "C_Cpp.clang_format_fallbackStyle": "LLVM",
4
  "diffEditor.ignoreTrimWhitespace": true,
5
  "editor.formatOnSave": false,
6
  "editor.tabSize": 2,
7
  "editor.wordWrap": "bounded",
8
  "editor.wordWrapColumn": 100,
9
  "files.associations": { },
10
}
0 11

  
lab3/.clang-format
1
BasedOnStyle: LLVM
2
AccessModifierOffset: -2
3
AlignAfterOpenBracket: true
4
AlignEscapedNewlinesLeft: false
5
AlignOperands: true
6
AlignTrailingComments: true
7
AllowAllParametersOfDeclarationOnNextLine: true
8
AllowShortBlocksOnASingleLine: true
9
AllowShortCaseLabelsOnASingleLine: true
10
AllowShortFunctionsOnASingleLine: All
11
AllowShortIfStatementsOnASingleLine: false
12
AllowShortLoopsOnASingleLine: true
13
AlwaysBreakAfterDefinitionReturnType: false
14
AlwaysBreakBeforeMultilineStrings: false
15
AlwaysBreakTemplateDeclarations: false
16
BinPackArguments: true
17
BinPackParameters: true
18
BraceWrapping:
19
  AfterClass:      false
20
  AfterControlStatement: false
21
  AfterEnum:       false
22
  AfterFunction:   false
23
  AfterNamespace:  false
24
  AfterObjCDeclaration: true
25
  AfterStruct:     false
26
  AfterUnion:      false
27
  BeforeCatch:     true
28
  BeforeElse:      true
29
  IndentBraces:    false
30
BreakBeforeBinaryOperators: None
31
BreakBeforeBraces: Custom
32
BreakBeforeTernaryOperators: true
33
BreakConstructorInitializersBeforeComma: false
34
ColumnLimit: 0
35
CommentPragmas: '^ IWYU pragma:'
36
ConstructorInitializerAllOnOneLineOrOnePerLine: false
37
ConstructorInitializerIndentWidth: 2
38
ContinuationIndentWidth: 2
39
Cpp11BracedListStyle: true
40
DerivePointerAlignment: false
41
DisableFormat: false
42
ExperimentalAutoDetectBinPacking: false
43
IndentCaseLabels: true
44
IndentPPDirectives: AfterHash
45
IndentWidth: 2
46
IndentWrappedFunctionNames: false
47
KeepEmptyLinesAtTheStartOfBlocks: true
48
Language: Cpp
49
MaxEmptyLinesToKeep: 1
50
NamespaceIndentation: None
51
ObjCBlockIndentWidth: 2
52
ObjCSpaceAfterProperty: false
53
ObjCSpaceBeforeProtocolList: true
54
PenaltyBreakBeforeFirstCallParameter: 19
55
PenaltyBreakComment: 300
56
PenaltyBreakFirstLessLess: 120
57
PenaltyBreakString: 1000
58
PenaltyExcessCharacter: 1000000
59
PenaltyReturnTypeOnItsOwnLine: 60
60
PointerAlignment: Right
61
SpaceAfterCStyleCast: true
62
SpaceBeforeAssignmentOperators: true
63
SpaceBeforeParens: ControlStatements
64
SpaceInEmptyParentheses: false
65
SpacesBeforeTrailingComments: 1
66
SpacesInAngles: false
67
SpacesInCStyleCastParentheses: false
68
SpacesInContainerLiterals: true
69
SpacesInParentheses: false
70
SpacesInSquareBrackets: false
71
Standard: Cpp11
72
TabWidth: 2
73
UseTab: Never
0 74

  
lab3/Makefile
1
# name of the program (Minix service)
2
PROG=lab3
3

  
4
# source code files to be compiled
5
SRCS = lab3.c 
6

  
7
# additional compilation flags
8
# "-Wall -Wextra -Werror -I . -std=c11 -Wno-unused-parameter" are already set
9
CFLAGS += -pedantic
10

  
11
# list of library dependencies (for Lab 2, only LCF library)
12
DPADD += ${LIBLCF}
13
LDADD += -llcf
14

  
15
# include LCOM's makefile that does all the "heavy lifting"
16
.include <minix.lcom.mk>
0 17

  
lab3/i8254.h
1
#ifndef _LCOM_I8254_H_
2
#define _LCOM_I8254_H_
3

  
4
#include <lcom/lcf.h>
5

  
6
/** @defgroup i8254 i8254
7
 * @{
8
 *
9
 * Constants for programming the i8254 Timer. Needs to be completed.
10
 */
11

  
12

  
13

  
14
/*IRQ's*/
15

  
16
#define IRQ_TIMER 0
17
#define DEFAULT_FREQ 60
18

  
19

  
20
#define TIMER_FREQ 1193182 /**< @brief clock frequency for timer in PC and AT */
21
#define MIN_FREQ 19
22
#define TIMER0_IRQ 0 /**< @brief Timer 0 IRQ line */
23

  
24
/* I/O port addresses */
25

  
26
#define TIMER_0    0x40 /**< @brief Timer 0 count register */
27
#define TIMER_1    0x41 /**< @brief Timer 1 count register */
28
#define TIMER_2    0x42 /**< @brief Timer 2 count register */
29
#define TIMER_CTRL 0x43 /**< @brief Control register */
30

  
31
#define SPEAKER_CTRL 0x61 /**< @brief Register for speaker control  */
32

  
33
/* Timer control */
34

  
35
/* Timer selection: bits 7 and 6 */
36

  
37
#define TIMER_SEL0   0x00              /**< @brief Control Word for Timer 0 */
38
#define TIMER_SEL1   BIT(6)            /**< @brief Control Word for Timer 1 */
39
#define TIMER_SEL2   BIT(7)            /**< @brief Control Word for Timer 2 */
40
#define TIMER_RB_CMD (BIT(7) | BIT(6)) /**< @brief Read Back Command */
41

  
42
/* Register selection: bits 5 and 4 */
43

  
44
#define TIMER_LSB     BIT(4)                  /**< @brief Initialize Counter LSB only */
45
#define TIMER_MSB     BIT(5)                  /**< @brief Initialize Counter MSB only */
46
#define TIMER_LSB_MSB (TIMER_LSB | TIMER_MSB) /**< @brief Initialize LSB first and MSB afterwards */
47

  
48
/* Operating mode: bits 3, 2 and 1 */
49

  
50
#define TIMER_SQR_WAVE (BIT(2) | BIT(1)) /**< @brief Mode 3: square wave generator */
51
#define TIMER_RATE_GEN BIT(2)            /**< @brief Mode 2: rate generator */
52

  
53
/* Counting mode: bit 0 */
54

  
55
#define TIMER_BCD 0x01 /**< @brief Count in BCD */
56
#define TIMER_BIN 0x00 /**< @brief Count in binary */
57

  
58
/* READ-BACK COMMAND FORMAT */
59

  
60
#define TIMER_RB_COUNT_  BIT(5)
61
#define TIMER_RB_STATUS_ BIT(4)
62
#define TIMER_RB_SEL(n)  BIT((n) + 1)
63

  
64
/**@}*/
65

  
66
#endif /* _LCOM_I8254_H */
0 67

  
lab3/lab2.c
1
#include <lcom/lcf.h>
2
#include <lcom/lab2.h>
3

  
4
#include <stdbool.h>
5
#include <stdint.h>
6

  
7
#include "i8254.h"
8
//#include "timer.c"
9
extern uint32_t cont;
10

  
11

  
12
int main(int argc, char *argv[]) {
13
  
14
  // sets the language of LCF messages (can be either EN-US or PT-PT)
15
  lcf_set_language("EN-US");
16

  
17
  // enables to log function invocations that are being "wrapped" by LCF
18
  // [comment this out if you don't want/need it]
19
  lcf_trace_calls("/home/lcom/labs/lab2/trace.txt");
20

  
21
  // enables to save the output of printf function calls on a file
22
  // [comment this out if you don't want/need it]
23
  lcf_log_output("/home/lcom/labs/lab2/output.txt");
24

  
25
  // handles control over to LCF
26
  // [LCF handles command line arguments and invokes the right function]
27
  if (lcf_start(argc, argv))
28
    return 1;
29

  
30
  // LCF clean up tasks
31
  // [must be the last statement before return]
32
  lcf_cleanup();
33

  
34
  return 0;
35
}
36

  
37
int(timer_test_read_config)(uint8_t timer, enum timer_status_field field) {
38
  /* To be implemented by the students */
39
  uint8_t st;
40
  if(timer_get_conf(timer,&st)==1) return 1;
41
  if(timer_display_conf(timer,st,field)==1) return 1;
42

  
43
  return 0;
44
 // printf("%s is not yet implemented!\n", __func__);
45

  
46
}
47

  
48
int(timer_test_time_base)(uint8_t timer, uint32_t freq) {
49
  /* To be implemented by the students */
50
  if(freq<MIN_FREQ || freq>TIMER_FREQ)
51
  {
52
    printf("OUT OF RANGE FREQUENCY\n");
53
    return 1;
54
  }
55
  return(timer_set_frequency(timer,freq));
56
  //printf("%s is not yet implemented!\n", __func__);
57

  
58
}
59

  
60
int(timer_test_int)(uint8_t time) {
61
  /* To be implemented by the students */
62

  
63
  if(time<0) 
64
  {
65
    printf("INVALID TIME!");
66
  return 1;
67
  }
68

  
69
  int ipc_status;
70
  message msg;
71
  uint8_t r;
72
  uint8_t bit_no = 0;
73
  timer_subscribe_int(&bit_no);
74
  while( (cont/DEFAULT_FREQ)<time )
75
 { /* You may want to use a different condition */
76
     /* Get a request message. */
77
     // printf("1\n");
78
     //printf("cont:%d, time:%d\n\n",cont/60,time);
79
     if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) 
80
     { 
81
        printf("driver_receive failed with: %d", r);
82
       continue;
83
    }
84
    if (is_ipc_notify(ipc_status)) { /* received notification */
85
        switch (_ENDPOINT_P(msg.m_source)) {
86
            case HARDWARE: /* hardware interrupt notification */				
87
                if (msg.m_notify.interrupts  & BIT(0)) { /* subscribed interrupt */
88
                   timer_int_handler();
89
                   if(cont%DEFAULT_FREQ==0)
90
                   timer_print_elapsed_time();
91
                   //printf("cont:%d\n",cont);
92
               }
93
                //printf("msg:%x\n",msg.m_notify.interrupts);
94
                break;
95
            default:
96
                //printf("2\n");
97
                break; /* no other notifications expected: do nothing */	
98
        }
99
    } else {
100
     // printf("3\n"); /* received a standard message, not a notification */
101
        /* no standard messages expected: do nothing */
102
    }
103
 }
104
 if(timer_unsubscribe_int()!=0) return 1;
105

  
106

  
107
  //printf("%s is not yet implemented!\n", __func__);
108

  
109
  return 0;
110
}
111
//dois primeiros bits sao 1, (TIMER_RB_CNTRL|BIT(5))
112
//SYS_OUTB para controlo->0x43, Config timer 0, dizer que queremos escrever para o controlo, e depois ler a parte corresponder
113
//g9
0 114

  
lab3/lab3.c
1
#include <lcom/lcf.h>
2
#include <minix/sysutil.h>
3

  
4
#include <lcom/lab3.h>
5

  
6
#include <stdbool.h>
7
#include <stdint.h>
8
//#include <timer.c>
9

  
10
#define IRQ_KB 1
11
#define DELAY_US 20000
12
#define EXIT_CODE 0x81
13
#define I_O_BUFFER 0x60
14

  
15
static uint32_t scancode;
16

  
17
int main(int argc, char *argv[]) {
18
  // sets the language of LCF messages (can be either EN-US or PT-PT)
19
  lcf_set_language("EN-US");
20

  
21
  // enables to log function invocations that are being "wrapped" by LCF
22
  // [comment this out if you don't want/need it]
23
  lcf_trace_calls("/home/lcom/labs/lab3/trace.txt");
24

  
25
  // enables to save the output of printf function calls on a file
26
  // [comment this out if you don't want/need it]
27
  lcf_log_output("/home/lcom/labs/lab3/output.txt");
28

  
29
  // handles control over to LCF
30
  // [LCF handles command line arguments and invokes the right function]
31
  if (lcf_start(argc, argv))
32
    return 1;
33

  
34
  // LCF clean up tasks
35
  // [must be the last statement before return]
36
  lcf_cleanup();
37

  
38
  return 0;
39
}
40

  
41
void (kbc_ih)()
42
{
43
  sys_inb(I_O_BUFFER, &scancode);
44
}
45

  
46
int(kbd_test_scan)() {
47
  /* To be completed by the students */
48
  int ipc_status;
49
  uint8_t r, bit_no=0;
50
  message msg;
51
  int hook_id=bit_no;
52
  uint8_t scan_arr[2] = {0,0};
53
  scan_arr[0]=scancode & 0xFF;
54
  scan_arr[1]=scancode & 0xFF00;
55
  sys_irqsetpolicy(IRQ_KB,IRQ_REENABLE|IRQ_EXCLUSIVE,&hook_id);
56

  
57
  //kbc_ih();
58
  printf("%x\n",scancode);
59
   while( 1)//(scancode & 0xFF) != EXIT_CODE )
60
 { /* You may want to use a different condition */
61
     /* Get a request message. */
62
     // printf("1\n");
63
     //printf("cont:%d, time:%d\n\n",cont/60,time);
64
     if ( (r = driver_receive(ANY, &msg, &ipc_status)) != 0 ) 
65
     { 
66
        printf("driver_receive failed with: %d", r);
67
       continue;
68
    }
69
    if (is_ipc_notify(ipc_status)) { /* received notification */
70
        switch (_ENDPOINT_P(msg.m_source)) {
71
            case HARDWARE: /* hardware interrupt notification */				
72
                if (msg.m_notify.interrupts  & BIT(0)) { /* subscribed interrupt */
73
                   
74
                   kbd_print_scancode((scancode & 0xFF & 0x80),2,scan_arr );
75
               }
76
                //printf("msg:%x\n",msg.m_notify.interrupts);
77
                break;
78
            default:
79
                //printf("2\n");
80
                break; /* no other notifications expected: do nothing */	
81
        }
82
    } else {
83
     // printf("3\n"); /* received a standard message, not a notification */
84
        /* no standard messages expected: do nothing */
85
    }
86
 }
87
 if(timer_unsubscribe_int()!=0) return 1;
88
sys_irqrmpolicy(&hook_id);
89

  
90

  
91
  return 0;
92
}
93

  
94
int(kbd_test_poll)() {
95
  /* To be completed by the students */
96
  printf("%s is not yet implemented!\n", __func__);
97

  
98
  return 1;
99
}
100

  
101
int(kbd_test_timed_scan)(uint8_t n) {
102
  /* To be completed by the students */
103
  printf("%s is not yet implemented!\n", __func__);
104

  
105
  return 1;
106
}
0 107

  
lab3/timer.c
1
#include <lcom/lcf.h>
2
#include <lcom/timer.h>
3

  
4
#include <stdint.h>
5

  
6
#include "i8254.h"
7

  
8
uint32_t cont=0;
9
int hook_id;
10

  
11

  
12
int (timer_set_frequency)(uint8_t timer, uint32_t freq) {
13
  /* To be implemented by the students */
14
  uint8_t conf;
15
  timer_get_conf(timer,&conf);
16
  uint32_t tudo;
17
  uint8_t msb,lsb;
18
  tudo=(TIMER_FREQ/freq);
19
  util_get_MSB(tudo,&msb);
20
  util_get_LSB(tudo,&lsb);
21
  
22
  //printf("%x\n%x\n%x\n%d\n",tudo,msb,lsb,timer);
23

  
24

  
25
  if(timer==0)
26
  {
27
    conf=(conf&0xF);
28
    conf = (conf  | TIMER_SEL0 | TIMER_LSB_MSB);
29
  if(sys_outb(TIMER_CTRL, conf)!=0) return 1;
30
  if(sys_outb(TIMER_0, lsb)!=0) return 1;
31
  if(sys_outb(TIMER_0, msb)!=0) return 1;
32
  }
33

  
34
  if(timer==1)
35
  {
36
    conf=(conf&0xF);
37
    conf = (conf | TIMER_SEL1 | TIMER_LSB_MSB);
38
  if(sys_outb(TIMER_CTRL, conf)!=0) return 1;
39
  if(sys_outb(TIMER_1, lsb)!=0) return 1;
40
  if(sys_outb(TIMER_1, msb)!=0) return 1;
41
  }
42

  
43
  if(timer==2)
44
  {
45
    conf=(conf&0xF);
46
    conf = (conf | TIMER_SEL2 | TIMER_LSB_MSB);
47
  if(sys_outb(TIMER_CTRL, conf)!=0) return 1;
48
  if(sys_outb(TIMER_2, lsb)!=0) return 1;
49
  if(sys_outb(TIMER_2, msb)!=0) return 1;
50
  }
51
  return 0;
52

  
53
}
54

  
55
int (timer_subscribe_int)(uint8_t *bit_no) {
56
    /* To be implemented by the students */
57
  //printf("%s is not yet implemented!\n", __func__);
58
 /*  int policy;
59
  sys_irqrmpolicy(&policy); */
60
  hook_id=*bit_no;
61
  if(sys_irqsetpolicy(IRQ_TIMER,IRQ_REENABLE,&hook_id)!=0) return 1;
62

  
63
  return 0;
64
}
65

  
66
int (timer_unsubscribe_int)() {
67
  /* To be implemented by the students */
68
// printf("%s is not yet implemented!\n", __func__);
69

  
70
  if(sys_irqrmpolicy(&hook_id)!=0) return 1;
71

  
72
  return 0;
73
}
74

  
75
void (timer_int_handler)() {
76
  /* To be implemented by the students */
77
  //printf("%s is not yet implemented!\n", __func__);
78
  cont=cont+1;
79
}
80

  
81
int (timer_get_conf)(uint8_t timer, uint8_t *st) {
82
  /* To be implemented by the students */
83

  
84
  uint8_t rbc = (TIMER_RB_CMD|TIMER_RB_SEL(4)|TIMER_RB_SEL(timer));
85
  if(sys_outb(TIMER_CTRL,rbc)!=0) return 1;
86

  
87
  
88
  if(timer==0)
89
  return (util_sys_inb(TIMER_0,st));
90
  if(timer==1)
91
  return (util_sys_inb(TIMER_1,st));
92
  if(timer==2)
93
  return (util_sys_inb(TIMER_2,st));
94

  
95
  return 1;
96

  
97
  //printf("%s is not yet implemented!\n", __func__);
98

  
99
  
100
}
101

  
102
int (timer_display_conf)(uint8_t timer, uint8_t st,
103
                        enum timer_status_field field) {
104
  /* To be implemented by the students */
105
  union timer_status_field_val val;
106
  
107

  
108
  if(field==tsf_all)
109
  {
110
    val.byte=st; 
111
    
112
  }
113
 if(field==tsf_initial)
114
  {
115
    //printf("%d\n",tsf_initial);
116

  
117
  if(((st&0x30)>>4)==0)
118
  val.in_mode=INVAL_val;
119

  
120
  if(((st&0x30)>>4)==1)
121
  val.in_mode=LSB_only;
122

  
123
  if(((st&0x30)>>4)==2)
124
  val.in_mode=MSB_only;
125

  
126
  if(((st&0x30)>>4)==3)
127
  val.in_mode=MSB_after_LSB;
128
  }
129
  
130

  
131
  if(field==tsf_mode)
132
  {
133
    //printf("%x\n",st);
134
    val.count_mode=((st&0xE)>>1);
135

  
136
    /* printf("%x\n",val.count_mode);
137
    val.count_mode=(val.count_mode>>1);
138
    printf("%x\n",val.count_mode); */
139
  //val.count_mode=(st&0x1C)/4;
140

  
141
 if (val.count_mode==6)
142
 val.count_mode=2;
143

  
144
 if (val.count_mode==7)
145
 val.count_mode=3;
146

  
147

  
148
  }
149

  
150
  if(field==tsf_base ) //BCD
151
  {
152
    //printf("%d\n",tsf_base);
153
    if((st&BIT(0))==1)
154
    val.bcd=true;
155

  
156
    else val.bcd=false;
157
  }
158

  
159

  
160
 return( timer_print_config(timer, field, val));
161
  //printf("%s is not yet implemented!\n", __func__);
162

  
163
  //return 0;
164
}
0 165

  
lab3/utils.c
1
#include <lcom/lcf.h>
2

  
3
#include <stdint.h>
4

  
5
int(util_get_LSB)(uint16_t val, uint8_t *lsb) {
6
  /* To be implemented by the students */
7
  //printf("%s is not yet implemented!\n", __func__);
8
  *lsb=val&0xFF;
9
  return 0;
10
}
11

  
12
int(util_get_MSB)(uint16_t val, uint8_t *msb) {
13
  /* To be implemented by the students */
14
  //printf("%s is not yet implemented!\n", __func__);
15
  *msb=(val&0xFF00)>>8;
16

  
17
  return 0;
18
}
19

  
20
int (util_sys_inb)(int port, uint8_t *value) {
21
  /* To be implemented by the students */
22
  uint32_t b32;
23
 if(sys_inb(port,&b32)!=0)return 1;
24
 //printf("%x",b32);
25
 *value=b32&0xFF;
26
 //printf("%x\n",*value);
27
  
28

  
29
  //printf("%s is not yet implemented!\n", __func__);
30

  
31
  return 0;
32
}
33

  
34
/* a = 133
35
b = a & 0x0F
36
1000 0101
37
 */
0 38

  

Also available in: Unified diff