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.

162 lines
3.2KB

  1. ; shell
  2. ;
  3. ; Runs a shell over an asynchronous communication interface adapter (ACIA).
  4. ; Incomplete. For now, this outputs a welcome prompt and then waits for input.
  5. ; Whenever input is CR or LF, we echo back what we've received and empty the
  6. ; input buffer. This also happen when the buffer is full.
  7. #include "platform.inc"
  8. ; *** CONSTS ***
  9. CR .equ 0x0d
  10. LF .equ 0x0a
  11. ; size of the input buffer. If our input goes over this size, we echo
  12. ; immediately.
  13. BUFSIZE .equ 0x20
  14. ; *** VARIABLES ***
  15. ; Our input buffer starts there
  16. INPTBUF .equ RAMSTART
  17. ; index, in the buffer, where our next character will go. 0 when the buffer is
  18. ; empty, BUFSIZE-1 when it's almost full.
  19. BUFIDX .equ INPTBUF+BUFSIZE
  20. ; *** CODE ***
  21. jr init
  22. init:
  23. ; disable interrupts we don't need them
  24. di
  25. ; setup stack
  26. ld hl, RAMEND
  27. ld sp, hl
  28. ; initialize variables
  29. xor a
  30. ld (BUFIDX), a ; starts at 0
  31. ; setup ACIA
  32. ; CR7 (1) - Receive Interrupt disabled
  33. ; CR6:5 (00) - RTS low, transmit interrupt disabled.
  34. ; CR4:2 (101) - 8 bits + 1 stop bit
  35. ; CR1:0 (10) - Counter divide: 64
  36. ld a, 0b00010110
  37. out (ACIA_CTL), a
  38. ; print prompt
  39. ld hl, d_welcome
  40. call printstr
  41. call printcrlf
  42. mainloop:
  43. call readc
  44. call chkbuf
  45. jr mainloop
  46. ; spits character in A in port SER_OUT
  47. printc:
  48. push af
  49. .stwait:
  50. in a, (ACIA_CTL) ; get status byte from SER
  51. bit 1, a ; are we still transmitting?
  52. jr z, .stwait ; if yes, wait until we aren't
  53. pop af
  54. out (ACIA_IO), a ; push current char
  55. ret
  56. ; print null-terminated string pointed to by HL
  57. printstr:
  58. ld a, (hl) ; load character to send
  59. or a ; is it zero?
  60. ret z ; if yes, we're finished
  61. call printc
  62. inc hl
  63. jr printstr
  64. ; no ret because our only way out is ret z above
  65. printcrlf:
  66. ld a, CR
  67. call printc
  68. ld a, LF
  69. call printc
  70. ret
  71. ; wait until a char is read in the ACIA and put it in the read buffer
  72. readc:
  73. ; first thing first: is our input buffer full? If yes, we don't even
  74. ; bother reading the ACIA. Something is wrong: we don't process data
  75. ; fast enough.
  76. ld a, (BUFIDX)
  77. cp BUFSIZE
  78. ret z ; if BUFIDX == BUFSIZE, our buffer is full.
  79. call getbufptr
  80. ; increase our buf ptr while we still have it in A
  81. inc a
  82. ld (BUFIDX), a
  83. .loop:
  84. ; Read our character from ACIA into our BUFIDX
  85. in a, (ACIA_CTL)
  86. bit 0, a ; is our ACIA rcv buffer full?
  87. jr z, .loop ; no? loop
  88. in a, (ACIA_IO)
  89. ld (hl), a
  90. ret
  91. ; check if the input buffer is full or ends in CR or LF. If it does, prints it
  92. ; back and empty it.
  93. chkbuf:
  94. ld a, (BUFIDX)
  95. cp 0
  96. ret z ; BUFIDX is zero? nothing to check.
  97. cp BUFSIZE
  98. jr z, .do ; if BUFIDX == BUFSIZE? do!
  99. call getbufptr
  100. ; our previous char is in BUFIDX - 1. Fetch this
  101. dec hl
  102. ld a, (hl) ; now, that's our char we have in A
  103. inc hl ; put HL back where it was
  104. cp CR
  105. jr z, .do ; char is CR? do!
  106. cp LF
  107. jr z, .do ; char is LF? do!
  108. ; nothing matched? don't do anything
  109. ret
  110. .do:
  111. ; terminate our string with 0
  112. xor a
  113. ld (hl), a
  114. ; reset buffer index
  115. ld (BUFIDX), a
  116. ; alright, let's go!
  117. ld hl, INPTBUF
  118. call printstr
  119. call printcrlf
  120. ret
  121. ; Set current buffer pointer in HL. The buffer pointer is where our *next* char
  122. ; will be written.
  123. getbufptr:
  124. ld hl, INPTBUF
  125. xor b
  126. ld c, a
  127. add hl, bc ; hl now points to INPTBUF + BUFIDX
  128. ret
  129. ; *** DATA ***
  130. d_welcome: .byte "Welcome to Collapse OS", 0