《操作系统》的实验代码。
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

455 lines
12 KiB

10 years ago
  1. #include <defs.h>
  2. #include <x86.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <kbdreg.h>
  6. #include <picirq.h>
  7. #include <trap.h>
  8. /* stupid I/O delay routine necessitated by historical PC design flaws */
  9. static void
  10. delay(void) {
  11. inb(0x84);
  12. inb(0x84);
  13. inb(0x84);
  14. inb(0x84);
  15. }
  16. /***** Serial I/O code *****/
  17. #define COM1 0x3F8
  18. #define COM_RX 0 // In: Receive buffer (DLAB=0)
  19. #define COM_TX 0 // Out: Transmit buffer (DLAB=0)
  20. #define COM_DLL 0 // Out: Divisor Latch Low (DLAB=1)
  21. #define COM_DLM 1 // Out: Divisor Latch High (DLAB=1)
  22. #define COM_IER 1 // Out: Interrupt Enable Register
  23. #define COM_IER_RDI 0x01 // Enable receiver data interrupt
  24. #define COM_IIR 2 // In: Interrupt ID Register
  25. #define COM_FCR 2 // Out: FIFO Control Register
  26. #define COM_LCR 3 // Out: Line Control Register
  27. #define COM_LCR_DLAB 0x80 // Divisor latch access bit
  28. #define COM_LCR_WLEN8 0x03 // Wordlength: 8 bits
  29. #define COM_MCR 4 // Out: Modem Control Register
  30. #define COM_MCR_RTS 0x02 // RTS complement
  31. #define COM_MCR_DTR 0x01 // DTR complement
  32. #define COM_MCR_OUT2 0x08 // Out2 complement
  33. #define COM_LSR 5 // In: Line Status Register
  34. #define COM_LSR_DATA 0x01 // Data available
  35. #define COM_LSR_TXRDY 0x20 // Transmit buffer avail
  36. #define COM_LSR_TSRE 0x40 // Transmitter off
  37. #define MONO_BASE 0x3B4
  38. #define MONO_BUF 0xB0000
  39. #define CGA_BASE 0x3D4
  40. #define CGA_BUF 0xB8000
  41. #define CRT_ROWS 25
  42. #define CRT_COLS 80
  43. #define CRT_SIZE (CRT_ROWS * CRT_COLS)
  44. #define LPTPORT 0x378
  45. static uint16_t *crt_buf;
  46. static uint16_t crt_pos;
  47. static uint16_t addr_6845;
  48. /* TEXT-mode CGA/VGA display output */
  49. static void
  50. cga_init(void) {
  51. volatile uint16_t *cp = (uint16_t *)CGA_BUF;
  52. uint16_t was = *cp;
  53. *cp = (uint16_t) 0xA55A;
  54. if (*cp != 0xA55A) {
  55. cp = (uint16_t*)MONO_BUF;
  56. addr_6845 = MONO_BASE;
  57. } else {
  58. *cp = was;
  59. addr_6845 = CGA_BASE;
  60. }
  61. // Extract cursor location
  62. uint32_t pos;
  63. outb(addr_6845, 14);
  64. pos = inb(addr_6845 + 1) << 8;
  65. outb(addr_6845, 15);
  66. pos |= inb(addr_6845 + 1);
  67. crt_buf = (uint16_t*) cp;
  68. crt_pos = pos;
  69. }
  70. static bool serial_exists = 0;
  71. static void
  72. serial_init(void) {
  73. // Turn off the FIFO
  74. outb(COM1 + COM_FCR, 0);
  75. // Set speed; requires DLAB latch
  76. outb(COM1 + COM_LCR, COM_LCR_DLAB);
  77. outb(COM1 + COM_DLL, (uint8_t) (115200 / 9600));
  78. outb(COM1 + COM_DLM, 0);
  79. // 8 data bits, 1 stop bit, parity off; turn off DLAB latch
  80. outb(COM1 + COM_LCR, COM_LCR_WLEN8 & ~COM_LCR_DLAB);
  81. // No modem controls
  82. outb(COM1 + COM_MCR, 0);
  83. // Enable rcv interrupts
  84. outb(COM1 + COM_IER, COM_IER_RDI);
  85. // Clear any preexisting overrun indications and interrupts
  86. // Serial port doesn't exist if COM_LSR returns 0xFF
  87. serial_exists = (inb(COM1 + COM_LSR) != 0xFF);
  88. (void) inb(COM1+COM_IIR);
  89. (void) inb(COM1+COM_RX);
  90. if (serial_exists) {
  91. pic_enable(IRQ_COM1);
  92. }
  93. }
  94. static void
  95. lpt_putc_sub(int c) {
  96. int i;
  97. for (i = 0; !(inb(LPTPORT + 1) & 0x80) && i < 12800; i ++) {
  98. delay();
  99. }
  100. outb(LPTPORT + 0, c);
  101. outb(LPTPORT + 2, 0x08 | 0x04 | 0x01);
  102. outb(LPTPORT + 2, 0x08);
  103. }
  104. /* lpt_putc - copy console output to parallel port */
  105. static void
  106. lpt_putc(int c) {
  107. if (c != '\b') {
  108. lpt_putc_sub(c);
  109. }
  110. else {
  111. lpt_putc_sub('\b');
  112. lpt_putc_sub(' ');
  113. lpt_putc_sub('\b');
  114. }
  115. }
  116. /* cga_putc - print character to console */
  117. static void
  118. cga_putc(int c) {
  119. // set black on white
  120. if (!(c & ~0xFF)) {
  121. c |= 0x0700;
  122. }
  123. switch (c & 0xff) {
  124. case '\b':
  125. if (crt_pos > 0) {
  126. crt_pos --;
  127. crt_buf[crt_pos] = (c & ~0xff) | ' ';
  128. }
  129. break;
  130. case '\n':
  131. crt_pos += CRT_COLS;
  132. case '\r':
  133. crt_pos -= (crt_pos % CRT_COLS);
  134. break;
  135. default:
  136. crt_buf[crt_pos ++] = c; // write the character
  137. break;
  138. }
  139. // What is the purpose of this?
  140. if (crt_pos >= CRT_SIZE) {
  141. int i;
  142. memmove(crt_buf, crt_buf + CRT_COLS, (CRT_SIZE - CRT_COLS) * sizeof(uint16_t));
  143. for (i = CRT_SIZE - CRT_COLS; i < CRT_SIZE; i ++) {
  144. crt_buf[i] = 0x0700 | ' ';
  145. }
  146. crt_pos -= CRT_COLS;
  147. }
  148. // move that little blinky thing
  149. outb(addr_6845, 14);
  150. outb(addr_6845 + 1, crt_pos >> 8);
  151. outb(addr_6845, 15);
  152. outb(addr_6845 + 1, crt_pos);
  153. }
  154. static void
  155. serial_putc_sub(int c) {
  156. int i;
  157. for (i = 0; !(inb(COM1 + COM_LSR) & COM_LSR_TXRDY) && i < 12800; i ++) {
  158. delay();
  159. }
  160. outb(COM1 + COM_TX, c);
  161. }
  162. /* serial_putc - print character to serial port */
  163. static void
  164. serial_putc(int c) {
  165. if (c != '\b') {
  166. serial_putc_sub(c);
  167. }
  168. else {
  169. serial_putc_sub('\b');
  170. serial_putc_sub(' ');
  171. serial_putc_sub('\b');
  172. }
  173. }
  174. /* *
  175. * Here we manage the console input buffer, where we stash characters
  176. * received from the keyboard or serial port whenever the corresponding
  177. * interrupt occurs.
  178. * */
  179. #define CONSBUFSIZE 512
  180. static struct {
  181. uint8_t buf[CONSBUFSIZE];
  182. uint32_t rpos;
  183. uint32_t wpos;
  184. } cons;
  185. /* *
  186. * cons_intr - called by device interrupt routines to feed input
  187. * characters into the circular console input buffer.
  188. * */
  189. static void
  190. cons_intr(int (*proc)(void)) {
  191. int c;
  192. while ((c = (*proc)()) != -1) {
  193. if (c != 0) {
  194. cons.buf[cons.wpos ++] = c;
  195. if (cons.wpos == CONSBUFSIZE) {
  196. cons.wpos = 0;
  197. }
  198. }
  199. }
  200. }
  201. /* serial_proc_data - get data from serial port */
  202. static int
  203. serial_proc_data(void) {
  204. if (!(inb(COM1 + COM_LSR) & COM_LSR_DATA)) {
  205. return -1;
  206. }
  207. int c = inb(COM1 + COM_RX);
  208. if (c == 127) {
  209. c = '\b';
  210. }
  211. return c;
  212. }
  213. /* serial_intr - try to feed input characters from serial port */
  214. void
  215. serial_intr(void) {
  216. if (serial_exists) {
  217. cons_intr(serial_proc_data);
  218. }
  219. }
  220. /***** Keyboard input code *****/
  221. #define NO 0
  222. #define SHIFT (1<<0)
  223. #define CTL (1<<1)
  224. #define ALT (1<<2)
  225. #define CAPSLOCK (1<<3)
  226. #define NUMLOCK (1<<4)
  227. #define SCROLLLOCK (1<<5)
  228. #define E0ESC (1<<6)
  229. static uint8_t shiftcode[256] = {
  230. [0x1D] CTL,
  231. [0x2A] SHIFT,
  232. [0x36] SHIFT,
  233. [0x38] ALT,
  234. [0x9D] CTL,
  235. [0xB8] ALT
  236. };
  237. static uint8_t togglecode[256] = {
  238. [0x3A] CAPSLOCK,
  239. [0x45] NUMLOCK,
  240. [0x46] SCROLLLOCK
  241. };
  242. static uint8_t normalmap[256] = {
  243. NO, 0x1B, '1', '2', '3', '4', '5', '6', // 0x00
  244. '7', '8', '9', '0', '-', '=', '\b', '\t',
  245. 'q', 'w', 'e', 'r', 't', 'y', 'u', 'i', // 0x10
  246. 'o', 'p', '[', ']', '\n', NO, 'a', 's',
  247. 'd', 'f', 'g', 'h', 'j', 'k', 'l', ';', // 0x20
  248. '\'', '`', NO, '\\', 'z', 'x', 'c', 'v',
  249. 'b', 'n', 'm', ',', '.', '/', NO, '*', // 0x30
  250. NO, ' ', NO, NO, NO, NO, NO, NO,
  251. NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
  252. '8', '9', '-', '4', '5', '6', '+', '1',
  253. '2', '3', '0', '.', NO, NO, NO, NO, // 0x50
  254. [0xC7] KEY_HOME, [0x9C] '\n' /*KP_Enter*/,
  255. [0xB5] '/' /*KP_Div*/, [0xC8] KEY_UP,
  256. [0xC9] KEY_PGUP, [0xCB] KEY_LF,
  257. [0xCD] KEY_RT, [0xCF] KEY_END,
  258. [0xD0] KEY_DN, [0xD1] KEY_PGDN,
  259. [0xD2] KEY_INS, [0xD3] KEY_DEL
  260. };
  261. static uint8_t shiftmap[256] = {
  262. NO, 033, '!', '@', '#', '$', '%', '^', // 0x00
  263. '&', '*', '(', ')', '_', '+', '\b', '\t',
  264. 'Q', 'W', 'E', 'R', 'T', 'Y', 'U', 'I', // 0x10
  265. 'O', 'P', '{', '}', '\n', NO, 'A', 'S',
  266. 'D', 'F', 'G', 'H', 'J', 'K', 'L', ':', // 0x20
  267. '"', '~', NO, '|', 'Z', 'X', 'C', 'V',
  268. 'B', 'N', 'M', '<', '>', '?', NO, '*', // 0x30
  269. NO, ' ', NO, NO, NO, NO, NO, NO,
  270. NO, NO, NO, NO, NO, NO, NO, '7', // 0x40
  271. '8', '9', '-', '4', '5', '6', '+', '1',
  272. '2', '3', '0', '.', NO, NO, NO, NO, // 0x50
  273. [0xC7] KEY_HOME, [0x9C] '\n' /*KP_Enter*/,
  274. [0xB5] '/' /*KP_Div*/, [0xC8] KEY_UP,
  275. [0xC9] KEY_PGUP, [0xCB] KEY_LF,
  276. [0xCD] KEY_RT, [0xCF] KEY_END,
  277. [0xD0] KEY_DN, [0xD1] KEY_PGDN,
  278. [0xD2] KEY_INS, [0xD3] KEY_DEL
  279. };
  280. #define C(x) (x - '@')
  281. static uint8_t ctlmap[256] = {
  282. NO, NO, NO, NO, NO, NO, NO, NO,
  283. NO, NO, NO, NO, NO, NO, NO, NO,
  284. C('Q'), C('W'), C('E'), C('R'), C('T'), C('Y'), C('U'), C('I'),
  285. C('O'), C('P'), NO, NO, '\r', NO, C('A'), C('S'),
  286. C('D'), C('F'), C('G'), C('H'), C('J'), C('K'), C('L'), NO,
  287. NO, NO, NO, C('\\'), C('Z'), C('X'), C('C'), C('V'),
  288. C('B'), C('N'), C('M'), NO, NO, C('/'), NO, NO,
  289. [0x97] KEY_HOME,
  290. [0xB5] C('/'), [0xC8] KEY_UP,
  291. [0xC9] KEY_PGUP, [0xCB] KEY_LF,
  292. [0xCD] KEY_RT, [0xCF] KEY_END,
  293. [0xD0] KEY_DN, [0xD1] KEY_PGDN,
  294. [0xD2] KEY_INS, [0xD3] KEY_DEL
  295. };
  296. static uint8_t *charcode[4] = {
  297. normalmap,
  298. shiftmap,
  299. ctlmap,
  300. ctlmap
  301. };
  302. /* *
  303. * kbd_proc_data - get data from keyboard
  304. *
  305. * The kbd_proc_data() function gets data from the keyboard.
  306. * If we finish a character, return it, else 0. And return -1 if no data.
  307. * */
  308. static int
  309. kbd_proc_data(void) {
  310. int c;
  311. uint8_t data;
  312. static uint32_t shift;
  313. if ((inb(KBSTATP) & KBS_DIB) == 0) {
  314. return -1;
  315. }
  316. data = inb(KBDATAP);
  317. if (data == 0xE0) {
  318. // E0 escape character
  319. shift |= E0ESC;
  320. return 0;
  321. } else if (data & 0x80) {
  322. // Key released
  323. data = (shift & E0ESC ? data : data & 0x7F);
  324. shift &= ~(shiftcode[data] | E0ESC);
  325. return 0;
  326. } else if (shift & E0ESC) {
  327. // Last character was an E0 escape; or with 0x80
  328. data |= 0x80;
  329. shift &= ~E0ESC;
  330. }
  331. shift |= shiftcode[data];
  332. shift ^= togglecode[data];
  333. c = charcode[shift & (CTL | SHIFT)][data];
  334. if (shift & CAPSLOCK) {
  335. if ('a' <= c && c <= 'z')
  336. c += 'A' - 'a';
  337. else if ('A' <= c && c <= 'Z')
  338. c += 'a' - 'A';
  339. }
  340. // Process special keys
  341. // Ctrl-Alt-Del: reboot
  342. if (!(~shift & (CTL | ALT)) && c == KEY_DEL) {
  343. cprintf("Rebooting!\n");
  344. outb(0x92, 0x3); // courtesy of Chris Frost
  345. }
  346. return c;
  347. }
  348. /* kbd_intr - try to feed input characters from keyboard */
  349. static void
  350. kbd_intr(void) {
  351. cons_intr(kbd_proc_data);
  352. }
  353. static void
  354. kbd_init(void) {
  355. // drain the kbd buffer
  356. kbd_intr();
  357. pic_enable(IRQ_KBD);
  358. }
  359. /* cons_init - initializes the console devices */
  360. void
  361. cons_init(void) {
  362. cga_init();
  363. serial_init();
  364. kbd_init();
  365. if (!serial_exists) {
  366. cprintf("serial port does not exist!!\n");
  367. }
  368. }
  369. /* cons_putc - print a single character @c to console devices */
  370. void
  371. cons_putc(int c) {
  372. lpt_putc(c);
  373. cga_putc(c);
  374. serial_putc(c);
  375. }
  376. /* *
  377. * cons_getc - return the next input character from console,
  378. * or 0 if none waiting.
  379. * */
  380. int
  381. cons_getc(void) {
  382. int c;
  383. // poll for any pending input characters,
  384. // so that this function works even when interrupts are disabled
  385. // (e.g., when called from the kernel monitor).
  386. serial_intr();
  387. kbd_intr();
  388. // grab the next character from the input buffer.
  389. if (cons.rpos != cons.wpos) {
  390. c = cons.buf[cons.rpos ++];
  391. if (cons.rpos == CONSBUFSIZE) {
  392. cons.rpos = 0;
  393. }
  394. return c;
  395. }
  396. return 0;
  397. }