Palacios Public Git Repository

To checkout Palacios execute

  git clone http://v3vee.org/palacios/palacios.web/palacios.git
This will give you the master branch. You probably want the devel branch or one of the release branches. To switch to the devel branch, simply execute
  cd palacios
  git checkout --track -b devel origin/devel
The other branches are similar.


Initial revision
[palacios.git] / misc / test_vm / src / geekos / serial.c
1 #include <geekos/serial.h>
2 #include <geekos/reboot.h>
3 #include <geekos/gdt.h>
4 #include <geekos/idt.h>
5
6
7
8
9 unsigned short serial_io_addr = 0;
10
11
12 static void Serial_Interrupt_Handler(struct Interrupt_State * state) {
13   char rcv_byte;
14   char irq_id;
15
16   Begin_IRQ(state);
17
18   irq_id = In_Byte(serial_io_addr + 2);
19
20
21   if ((irq_id & 0x04) != 0) {
22     rcv_byte = In_Byte(serial_io_addr + 0);
23
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);
30       DumpIDT();
31       DumpGDT();
32     }
33       
34 #if 0
35       SerialPrint("Unreserved serial byte: %d (%c)\r\n", rcv_byte, rcv_byte);
36 #endif
37   }
38   End_IRQ(state);
39 }
40
41 void InitSerial() {
42   Print("Initialzing Serial\n");
43   Install_IRQ(COM1_IRQ, Serial_Interrupt_Handler);
44   Enable_IRQ(COM1_IRQ);
45   InitSerialAddr(DEFAULT_SERIAL_ADDR);
46 }
47
48 void InitSerialAddr(unsigned short io_addr) {
49   serial_io_addr = io_addr;
50
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);
57   /* 8N1 */
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);
68 }
69
70
71 inline static void SerialPutChar(unsigned char c) {
72  
73  //  static unsigned short io_adr;
74   if (serial_io_addr==0) { 
75     return;
76   }
77
78
79   if (c=='\n') { 
80     /* wait for transmitter ready */
81     while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
82     }
83     /* send char */
84     Out_Byte(serial_io_addr + 0, '\r');
85     /* wait for transmitter ready */
86   }
87   while((In_Byte(serial_io_addr + 5) & 0x40) == 0) {
88   }
89   /* send char */
90   Out_Byte(serial_io_addr + 0, c);
91 }
92
93
94
95 void SerialPutLineN(char * line, int len) {
96   int i;
97   for (i = 0; i < len && line[i] != 0; i++) { 
98     SerialPutChar(line[i]); 
99   }
100 }
101
102
103 void SerialPutLine(char * line) {
104   int i;
105   for (i = 0; line[i]!= 0; i++) { 
106     SerialPutChar(line[i]); 
107   }
108 }
109
110
111 void SerialPrintHex(unsigned char x)
112 {
113   unsigned char z;
114   
115   z = (x>>4) & 0xf ;
116   SerialPrint("%x", z);
117   z = x & 0xf;
118   SerialPrint("%x", z);
119 }
120
121 void SerialMemDump(unsigned char *start, int n)
122 {
123   int i, j;
124
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) {
128       SerialPrint(" ");
129       SerialPrintHex(*((unsigned char *)(start+j)));
130       if ((j+1)<n) { 
131         SerialPrintHex(*((unsigned char *)(start+j+1)));
132       }
133     }
134     SerialPrint(" ");
135     for (j=i; j<i+16 && j<n;j++) {
136       SerialPrint("%c", ((start[j]>=32) && (start[j]<=126)) ? start[j] : '.');
137     }
138     SerialPrint("\n");
139   }
140 }