Mirror of CollapseOS
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.

146 lines
3.2KB

  1. ; stdio
  2. ;
  3. ; Allows other modules to print to "standard out", and get data from "standard
  4. ; in", that is, the console through which the user is connected in a decoupled
  5. ; manner.
  6. ;
  7. ; Those GetC/PutC routines are hooked through defines and have this API:
  8. ;
  9. ; GetC: Blocks until a character is read from the device and return that
  10. ; character in A.
  11. ;
  12. ; PutC: Write character specified in A onto the device.
  13. ;
  14. ; *** Accepted characters ***
  15. ;
  16. ; For now, we're in muddy waters in this regard. We try to stay close to ASCII.
  17. ; Anything over 0x7f is undefined. Both CR and LF are interpreted as "line end".
  18. ; Both BS and DEL mean "delete previous character".
  19. ;
  20. ; When outputting, newlines are marked by CR and LF. Outputting a character
  21. ; deletion is made through BS then space then BS.
  22. ;
  23. ; *** Defines ***
  24. ; STDIO_GETC: address of a GetC routine
  25. ; STDIO_PUTC: address of a PutC routine
  26. ;
  27. ; *** Consts ***
  28. ; Size of the readline buffer. If a typed line reaches this size, the line is
  29. ; flushed immediately (same as pressing return).
  30. .equ STDIO_BUFSIZE 0x40
  31. ; *** Variables ***
  32. ; Line buffer. We read types chars into this buffer until return is pressed
  33. ; This buffer is null-terminated.
  34. .equ STDIO_BUF STDIO_RAMSTART
  35. ; Index where the next char will go in stdioGetC.
  36. .equ STDIO_RAMEND @+STDIO_BUFSIZE
  37. stdioGetC:
  38. jp STDIO_GETC
  39. stdioPutC:
  40. jp STDIO_PUTC
  41. ; print null-terminated string pointed to by HL
  42. printstr:
  43. push af
  44. push hl
  45. .loop:
  46. ld a, (hl) ; load character to send
  47. or a ; is it zero?
  48. jr z, .end ; if yes, we're finished
  49. call STDIO_PUTC
  50. inc hl
  51. jr .loop
  52. .end:
  53. pop hl
  54. pop af
  55. ret
  56. ; print B characters from string that HL points to
  57. printnstr:
  58. push bc
  59. push hl
  60. .loop:
  61. ld a, (hl) ; load character to send
  62. call STDIO_PUTC
  63. inc hl
  64. djnz .loop
  65. .end:
  66. pop hl
  67. pop bc
  68. ret
  69. printcrlf:
  70. push af
  71. ld a, CR
  72. call STDIO_PUTC
  73. ld a, LF
  74. call STDIO_PUTC
  75. pop af
  76. ret
  77. ; Repeatedly calls stdioGetC until a whole line was read, that is, when CR or
  78. ; LF is read or if the buffer is full. Sets HL to the beginning of the read
  79. ; line, which is null-terminated.
  80. ;
  81. ; This routine also takes care of echoing received characters back to the TTY.
  82. ; It also manages backspaces properly.
  83. stdioReadLine:
  84. push bc
  85. ld hl, STDIO_BUF
  86. ld b, STDIO_BUFSIZE-1
  87. .loop:
  88. ; Let's wait until something is typed.
  89. call STDIO_GETC
  90. ; got it. Now, is it a CR or LF?
  91. cp CR
  92. jr z, .complete ; char is CR? buffer complete!
  93. cp LF
  94. jr z, .complete
  95. cp DEL
  96. jr z, .delchr
  97. cp BS
  98. jr z, .delchr
  99. ; Echo the received character right away so that we see what we type
  100. call STDIO_PUTC
  101. ; Ok, gotta add it do the buffer
  102. ld (hl), a
  103. inc hl
  104. djnz .loop
  105. ; buffer overflow, complete line
  106. .complete:
  107. ; The line in our buffer is complete.
  108. ; Let's null-terminate it and return.
  109. xor a
  110. ld (hl), a
  111. ld hl, STDIO_BUF
  112. pop bc
  113. ret
  114. .delchr:
  115. ; Deleting is a tricky business. We have to decrease HL and increase B
  116. ; so that everything stays consistent. We also have to make sure that
  117. ; We don't do buffer underflows.
  118. ld a, b
  119. cp STDIO_BUFSIZE-1
  120. jr z, .loop ; beginning of line, nothing to delete
  121. dec hl
  122. inc b
  123. ; Char deleted in buffer, now send BS + space + BS for the terminal
  124. ; to clear its previous char
  125. ld a, BS
  126. call STDIO_PUTC
  127. ld a, ' '
  128. call STDIO_PUTC
  129. ld a, BS
  130. call STDIO_PUTC
  131. jr .loop