1 #include <geekos/serial.h>
2 #include <geekos/reboot.h>
3 #include <geekos/gdt.h>
4 #include <geekos/idt.h>
9 unsigned short serial_io_addr = 0;
12 static void Serial_Interrupt_Handler(struct Interrupt_State * state) {
18 irq_id = In_Byte(serial_io_addr + 2);
21 if ((irq_id & 0x04) != 0) {
22 rcv_byte = In_Byte(serial_io_addr + 0);
24 if (rcv_byte == 'k') {
25 SerialPrint("Restarting Machine\r\n");
26 machine_real_restart();
27 } else if (rcv_byte=='d') {
28 SerialPrint("Dumping Machine State\n");
29 Dump_Interrupt_State(state);
35 SerialPrint("Unreserved serial byte: %d (%c)\r\n", rcv_byte, rcv_byte);
42 Print("Initialzing Serial\n");
43 Install_IRQ(COM1_IRQ, Serial_Interrupt_Handler);
45 InitSerialAddr(DEFAULT_SERIAL_ADDR);
48 void InitSerialAddr(unsigned short io_addr) {
49 serial_io_addr = io_addr;
51 Print("Initializing Polled Serial Output on COM1 - 115200 N81 noflow\n");
52 // io_adr = 0x3F8; /* 3F8=COM1, 2F8=COM2, 3E8=COM3, 2E8=COM4 */
53 Out_Byte(io_addr + 3, 0x80);
54 // 115200 /* 115200 / 12 = 9600 baud */
55 Out_Byte(io_addr + 0, 1);
56 Out_Byte(io_addr + 1, 0);
58 Out_Byte(io_addr + 3, 0x03);
59 /* all interrupts disabled */
60 // Out_Byte(io_addr + 1, 0);
61 Out_Byte(io_addr + 1, 0x01);
62 /* turn off FIFO, if any */
63 Out_Byte(io_addr + 2, 0);
64 /* loopback off, interrupts (Out2) off, Out1/RTS/DTR off */
65 // Out_Byte(io_addr + 4, 0);
66 // enable interrupts (bit 3)
67 Out_Byte(io_addr + 4, 0x08);
71 inline static void SerialPutChar(unsigned char c) {
73 // static unsigned short io_adr;
74 if (serial_io_addr==0) {
80 /* wait for transmitter ready */
81 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
84 Out_Byte(serial_io_addr + 0, '\r');
85 /* wait for transmitter ready */
87 while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
90 Out_Byte(serial_io_addr + 0, c);
95 void SerialPutLineN(char * line, int len) {
97 for (i = 0; i < len && line[i] != 0; i++) {
98 SerialPutChar(line[i]);
103 void SerialPutLine(char * line) {
105 for (i = 0; line[i]!= 0; i++) {
106 SerialPutChar(line[i]);
111 void SerialPrintHex(unsigned char x)
116 SerialPrint("%x", z);
118 SerialPrint("%x", z);
121 void SerialMemDump(unsigned char *start, int n)
125 for (i=0;i<n;i+=16) {
126 SerialPrint("%8x", (unsigned)(start+i));
127 for (j=i; j<i+16 && j<n; j+=2) {
129 SerialPrintHex(*((unsigned char *)(start+j)));
131 SerialPrintHex(*((unsigned char *)(start+j+1)));
135 for (j=i; j<i+16 && j<n;j++) {
136 SerialPrint("%c", ((start[j]>=32) && (start[j]<=126)) ? start[j] : '.');