Project

General

Profile

Statistics
| Revision:

root / proj / proj.c @ 152

History | View | Annotate | Download (4.87 KB)

1 144 up20180655
#include <lcom/lcf.h>
2
#include <lcom/proj.h>
3
#include <lcom/liblm.h>
4
5 147 up20180655
#include <stdbool.h>
6 144 up20180655
#include <stdint.h>
7
8 149 up20180655
#include "i8254.h"
9
#include "kbc_macros.h"
10
#include "graphics_macros.h"
11 150 up20180655
#include "mouse_macros.h"
12 149 up20180655
#include "proj_macros.h"
13
#include "errors.h"
14 147 up20180655
15 149 up20180655
#include "kbc.h"
16
#include "graphics.h"
17
#include "timer.h"
18
#include "keyboard.h"
19 150 up20180655
#include "mouse.h"
20 149 up20180655
#include "utils.h"
21
22 144 up20180655
int main(int argc, char* argv[]) {
23
24
    lcf_set_language("EN-US");
25
26
    //lcf_trace_calls("/home/lcom/labs/proj/trace.txt");
27
28
    //lcf_log_output("/home/lcom/labs/proj/output.txt");
29
30
    if (lcf_start(argc, argv)) return 1;
31
32
    lcf_cleanup();
33
34
    return 0;
35
}
36 147 up20180655
37 149 up20180655
int(proj_main_loop)(int argc, char *argv[]) {
38 147 up20180655
39 152 up20180642
    if (vbe_get_mode_information(INDEXED_1024_768)) {
40
        printf("%s: failed to get information for mode %x.\n", __func__, INDEXED_1024_768);
41
        if (vg_exit())
42
            printf("%s: vg_exit failed to exit to text mode.\n", __func__);
43
        return 1;
44
    }
45
46
    map_vram(); // if function fails it aborts program
47
48
    if (set_graphics_mode(INDEXED_1024_768)) {
49
        printf("%s: failed to set graphic mode %x.\n", __func__, INDEXED_1024_768);
50
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
51
        if (free_memory_map()) {
52
            printf("%s: lm_free failed\n", __func__);
53
        }
54
        return 1;
55
    };
56
57
58 149 up20180655
    /// loop stuff
59
    int ipc_status, r;
60
    message msg;
61
    /// Keyboard interrupt handling
62
    uint8_t kbc_irq_bit = KBC_IRQ;
63
    int kbc_id = 0;
64
    int kbc_irq = BIT(kbc_irq_bit);
65
    if (subscribe_kbc_interrupt(kbc_irq_bit, &kbc_id)) {
66
        return 1;
67
    }
68 147 up20180655
69 149 up20180655
    /// Timer interrupt handling
70
    //const uint32_t frequency = sys_hz(); // Frequency asummed at 60Hz
71
    uint8_t timer_irq_bit = TIMER0_IRQ;
72
    int timer_id = 0;
73
    int timer_irq = BIT(timer_irq_bit);
74
    if(subscribe_timer_interrupt(timer_irq_bit, &timer_id)) {
75
        if (vg_exit()) printf("%s: vg_exit failed to exit to text mode.\n", __func__);
76
        if (free_memory_map()) {
77
            printf("%s: lm_free failed\n", __func__);
78
        }
79
        return 1;
80
    }
81
82 150 up20180655
    /// Mouse interrupt handling
83
    uint8_t mouse_irq_bit = MOUSE_IRQ;
84
    int mouse_id = 0;
85
    int mouse_irq = BIT(mouse_irq_bit);
86
87
    if (subscribe_mouse_interrupt(mouse_irq_bit, &mouse_id)) return 1; // subscribes mouse interrupts in exclusive mode
88
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
89
    if (mouse_set_data_report(true)) return 1; // enables mouse data reporting
90
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
91
92 147 up20180655
    /// cycle
93
    int good = 1;
94
    while (good) {
95
        /* Get a request message. */
96
        if ((r = driver_receive(ANY, &msg, &ipc_status)) != 0) {
97
            printf("driver_receive failed with %d", r);
98
            continue;
99
        }
100
        if (is_ipc_notify(ipc_status)) { /* received notification */
101
            switch (_ENDPOINT_P(msg.m_source)) {
102
                case HARDWARE: /* hardware interrupt notification */
103
                    if (msg.m_notify.interrupts & kbc_irq) { /* subscribed interrupt */
104
                        kbc_ih();
105
                        if (scancode[0] == ESC_BREAK_CODE) good = 0;
106
                    }
107 149 up20180655
                    if (msg.m_notify.interrupts & timer_irq) { /* subscribed interrupt */
108
                        timer_int_handler();
109
                    }
110 150 up20180655
111
                    if (msg.m_notify.interrupts & mouse_irq) { /* subscribed interrupt */
112
                        mouse_ih();
113
                    }
114 147 up20180655
                    break;
115
                default:
116
                    break; /* no other notifications expected: do nothing */
117
            }
118
        } else { /* received standart message, not a notification */
119
            /* no standart message expected: do nothing */
120
        }
121
    }
122 149 up20180655
123
    // Unsubscribe Keyboard interrupts
124
    if (unsubscribe_interrupt(&kbc_id)) {
125
        return 1;
126
    };
127
128
    // Unsubscribe Timer Interrupts
129
    if (unsubscribe_interrupt(&timer_id)) {
130
        return 1;
131
    }
132
133 150 up20180655
    // Unsubscribe Mouse Interrupts
134
    if (sys_irqdisable(&mouse_id)) return 1; // temporarily disables our interrupts notifications
135
    if (mouse_set_data_report(false)) return 1; // enables mouse data reporting
136
    if (sys_irqenable(&mouse_id)) return 1; // re-enables our interrupts notifications
137
    if (unsubscribe_interrupt(&mouse_id)) return 1; // unsubscribes interrupts
138
139 152 up20180642
140
    if (vg_exit()) {
141
        printf("%s: vg_exit failed to exit to text mode.\n", __func__);
142
        if (free_memory_map()) printf("%s: lm_free failed\n", __func__);
143
        return 1;
144
    }
145
146
    if (free_memory_map()) {
147
        printf("%s: lm_free failed\n", __func__);
148
        return 1;
149
    }
150
151
152
    #ifdef DIOGO
153
        hello
154
    #endif
155
156
157 149 up20180655
    return 0;
158 152 up20180642
159
    return 0;
160 147 up20180655
}