lscreen.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Driver for full-screen console.
  31. *
  32. * As of this writing the full-screen console is not supported in
  33. * System/161, so this driver is untested and probably broken.
  34. */
  35. #include <types.h>
  36. #include <lib.h>
  37. #include <spinlock.h>
  38. #include <platform/bus.h>
  39. #include <lamebus/lscreen.h>
  40. #include "autoconf.h"
  41. /* Registers (offsets within slot) */
  42. #define LSCR_REG_POSN 0 /* Cursor position */
  43. #define LSCR_REG_SIZE 4 /* Display size */
  44. #define LSCR_REG_CHAR 8 /* Character in */
  45. #define LSCR_REG_RIRQ 12 /* Read interrupt status */
  46. /* Bits in the IRQ registers */
  47. #define LSCR_IRQ_ENABLE 1
  48. #define LSCR_IRQ_ACTIVE 2
  49. /* Offset within slot of screen buffer */
  50. #define LSCR_SCREEN 32768
  51. /* Convert a 32-bit X/Y pair to X and Y coordinates. */
  52. static
  53. inline
  54. void
  55. splitxy(uint32_t xy, unsigned *x, unsigned *y)
  56. {
  57. *x = xy >> 16;
  58. *y = xy & 0xffff;
  59. }
  60. /* Convert X and Y coordinates to a single 32-bit value. */
  61. static
  62. inline
  63. uint32_t
  64. mergexy(unsigned x, unsigned y)
  65. {
  66. uint32_t val = x;
  67. return (val << 16) | y;
  68. }
  69. ////////////////////////////////////////////////////////////
  70. /*
  71. * Interrupt handler.
  72. */
  73. void
  74. lscreen_irq(void *vsc)
  75. {
  76. struct lscreen_softc *sc = vsc;
  77. uint32_t ch, x;
  78. spinlock_acquire(&sc->ls_lock);
  79. x = bus_read_register(sc->ls_busdata, sc->ls_buspos, LSCR_REG_RIRQ);
  80. if (x & LSCR_IRQ_ACTIVE) {
  81. ch = bus_read_register(sc->ls_busdata, sc->ls_buspos,
  82. LSCR_REG_CHAR);
  83. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  84. LSCR_REG_RIRQ, LSCR_IRQ_ENABLE);
  85. spinlock_release(&sc->ls_lock);
  86. if (sc->ls_input) {
  87. sc->ls_input(sc->ls_devdata, ch);
  88. }
  89. }
  90. else {
  91. spinlock_release(&sc->ls_lock);
  92. }
  93. }
  94. ////////////////////////////////////////////////////////////
  95. /*
  96. * Handle a newline on the screen.
  97. */
  98. static
  99. void
  100. lscreen_newline(struct lscreen_softc *sc)
  101. {
  102. if (sc->ls_cy >= sc->ls_height-1) {
  103. /*
  104. * Scroll
  105. */
  106. memmove(sc->ls_screen, sc->ls_screen + sc->ls_width,
  107. sc->ls_width * (sc->ls_height-1));
  108. bzero(sc->ls_screen + sc->ls_width * (sc->ls_height-1),
  109. sc->ls_width);
  110. }
  111. else {
  112. sc->ls_cy++;
  113. }
  114. sc->ls_cx=0;
  115. }
  116. /*
  117. * Handle a printable character being written to the screen.
  118. */
  119. static
  120. void
  121. lscreen_char(struct lscreen_softc *sc, int ch)
  122. {
  123. if (sc->ls_cx >= sc->ls_width) {
  124. lscreen_newline(sc);
  125. }
  126. sc->ls_screen[sc->ls_cy*sc->ls_width + sc->ls_cx] = ch;
  127. sc->ls_cx++;
  128. }
  129. /*
  130. * Send a character to the screen.
  131. * This should probably know about backspace and tab.
  132. */
  133. void
  134. lscreen_write(void *vsc, int ch)
  135. {
  136. struct lscreen_softc *sc = vsc;
  137. int ccx, ccy;
  138. spinlock_acquire(&sc->ls_lock);
  139. switch (ch) {
  140. case '\n': lscreen_newline(sc); break;
  141. default: lscreen_char(sc, ch); break;
  142. }
  143. /*
  144. * ccx/ccy = corrected cursor position
  145. * (The cursor marks the next space text will appear in. But
  146. * at the very end of the line, it should not move off the edge.)
  147. */
  148. ccx = sc->ls_cx;
  149. ccy = sc->ls_cy;
  150. if (ccx==sc->ls_width) {
  151. ccx--;
  152. }
  153. /* Set the cursor position */
  154. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  155. LSCR_REG_POSN, mergexy(ccx, ccy));
  156. spinlock_release(&sc->ls_lock);
  157. }
  158. ////////////////////////////////////////////////////////////
  159. /*
  160. * Setup routine called by autoconf.c when an lscreen is found.
  161. */
  162. int
  163. config_lscreen(struct lscreen_softc *sc, int lscreenno)
  164. {
  165. uint32_t val;
  166. (void)lscreenno;
  167. spinlock_init(&sc->ls_lock);
  168. /*
  169. * Enable interrupting.
  170. */
  171. bus_write_register(sc->ls_busdata, sc->ls_buspos,
  172. LSCR_REG_RIRQ, LSCR_IRQ_ENABLE);
  173. /*
  174. * Get screen size.
  175. */
  176. val = bus_read_register(sc->ls_busdata, sc->ls_buspos,
  177. LSCR_REG_SIZE);
  178. splitxy(val, &sc->ls_width, &sc->ls_height);
  179. /*
  180. * Get cursor position.
  181. */
  182. val = bus_read_register(sc->ls_busdata, sc->ls_buspos,
  183. LSCR_REG_POSN);
  184. splitxy(val, &sc->ls_cx, &sc->ls_cy);
  185. /*
  186. * Get a pointer to the memory-mapped screen area.
  187. */
  188. sc->ls_screen = bus_map_area(sc->ls_busdata, sc->ls_buspos,
  189. LSCR_SCREEN);
  190. return 0;
  191. }