Mirror of CollapseOS
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

198 řádky
7.1KB

  1. ( ----- 600 )
  2. Sega Master System Recipe
  3. 602 VDP 610 PAD
  4. 620 KBD 625 Ports
  5. ( ----- 602 )
  6. ( VDP Driver. requires TMS9918 driver. Load range B602-B604. )
  7. CREATE _idat
  8. 0b00000100 C, 0x80 C, ( Bit 2: Select mode 4 )
  9. 0b00000000 C, 0x81 C,
  10. 0b00001111 C, 0x82 C, ( Name table: 0x3800, *B0 must be 1* )
  11. 0b11111111 C, 0x85 C, ( Sprite table: 0x3f00 )
  12. 0b11111111 C, 0x86 C, ( sprite use tiles from 0x2000 )
  13. 0b11111111 C, 0x87 C, ( Border uses palette 0xf )
  14. 0b00000000 C, 0x88 C, ( BG X scroll )
  15. 0b00000000 C, 0x89 C, ( BG Y scroll )
  16. 0b11111111 C, 0x8a C, ( Line counter (why have this?) )
  17. ( ----- 603 )
  18. ( Each row in ~FNT is a row of the glyph and there is 7 of
  19. them. We insert a blank one at the end of those 7. For each
  20. row we set, we need to send 3 zero-bytes because each pixel in
  21. the tile is actually 4 bits because it can select among 16
  22. palettes. We use only 2 of them, which is why those bytes
  23. always stay zero. )
  24. : _sfont ( a -- Send font to VDP )
  25. 7 0 DO C@+ _data 3 _zero LOOP DROP
  26. ( blank row ) 4 _zero ;
  27. : CELL! ( c pos )
  28. 2 * 0x7800 OR _ctl ( c )
  29. 0x20 - ( glyph ) 0x5e MOD _data ;
  30. ( ----- 604 )
  31. : CURSOR! ( new old -- )
  32. ( unset palette bit in old tile )
  33. 2 * 1+ 0x7800 OR _ctl 0 _data
  34. ( set palette bit for at specified pos )
  35. 2 * 1+ 0x7800 OR _ctl 0x8 _data ;
  36. : VDP$
  37. 9 0 DO _idat I 2 * + @ _ctl LOOP
  38. ( blank screen ) 0x7800 _ctl COLS LINES * 2 * _zero
  39. ( palettes )
  40. 0xc000 _ctl
  41. ( BG ) 1 _zero 0x3f _data 14 _zero
  42. ( sprite, inverted colors ) 0x3f _data 15 _zero
  43. 0x4000 _ctl 0x5e 0 DO ~FNT I 7 * + _sfont LOOP
  44. ( bit 6, enable display, bit 7, ?? ) 0x81c0 _ctl ;
  45. : COLS 32 ; : LINES 24 ;
  46. ( ----- 610 )
  47. Pad driver - read input from MD controller
  48. Conveniently expose an API to read the status of a MD pad A.
  49. Moreover, implement a mechanism to input arbitrary characters
  50. from it. It goes as follow:
  51. * Direction pad select characters. Up/Down move by one,
  52. Left/Right move by 5
  53. * Start acts like Return
  54. * A acts like Backspace
  55. * B changes "character class": lowercase, uppercase, numbers,
  56. special chars. The space character is the first among special
  57. chars.
  58. * C confirms letter selection
  59. (cont.)
  60. ( ----- 611 )
  61. This module is currently hard-wired to VDP driver, that is, it
  62. calls vdp's routines during (key?) to update character
  63. selection.
  64. Load range: 632-637
  65. ( ----- 612 )
  66. : _prevstat [ PAD_MEM LITN ] ;
  67. : _sel [ PAD_MEM 1+ LITN ] ;
  68. : _next [ PAD_MEM 2+ LITN ] ;
  69. ( Put status for port A in register A. Bits, from MSB to LSB:
  70. Start - A - C - B - Right - Left - Down - Up
  71. Each bit is high when button is unpressed and low if button is
  72. pressed. When no button is pressed, 0xff is returned.
  73. This logic below is for the Genesis controller, which is modal.
  74. TH is an output pin that switches the meaning of TL and TR. When
  75. TH is high (unselected), TL = Button B and TR = Button C. When
  76. TH is low (selected), TL = Button A and TR = Start. )
  77. ( ----- 613 )
  78. : _status
  79. 1 _THA! ( output, high/unselected )
  80. _D1@ 0x3f AND ( low 6 bits are good )
  81. ( Start and A are returned when TH is selected, in bits 5 and
  82. 4. Well get them, left-shift them and integrate them to B. )
  83. 0 _THA! ( output, low/selected )
  84. _D1@ 0x30 AND 2 LSHIFT OR ;
  85. ( ----- 614 )
  86. : _chk ( c --, check _sel range )
  87. _sel C@ DUP 0x7f > IF 0x20 _sel C! THEN
  88. 0x20 < IF 0x7f _sel C! THEN ;
  89. CREATE _ '0' C, ':' C, 'A' C, '[' C, 'a' C, 0xff C,
  90. : _nxtcls
  91. _sel @ _ BEGIN ( c a ) C@+ 2 PICK > UNTIL ( c a )
  92. 1- C@ NIP _sel !
  93. ;
  94. ( ----- 615 )
  95. : _updsel ( -- f, has an action button been pressed? )
  96. _status _prevstat C@ OVER = IF DROP 0 EXIT THEN
  97. DUP _prevstat C! ( changed, update ) ( s )
  98. 0x01 ( UP ) OVER AND NOT IF 1 _sel +! THEN
  99. 0x02 ( DOWN ) OVER AND NOT IF -1 _sel +! THEN
  100. 0x04 ( LEFT ) OVER AND NOT IF -5 _sel +! THEN
  101. 0x08 ( RIGHT ) OVER AND NOT IF 5 _sel +! THEN
  102. 0x10 ( BUTB ) OVER AND NOT IF _nxtcls THEN
  103. ( update sel in VDP )
  104. _chk _sel C@ XYPOS @ CELL!
  105. ( return whether any of the high 3 bits is low )
  106. 0xe0 AND 0xe0 <
  107. ;
  108. ( ----- 616 )
  109. : (key?) ( -- c? f )
  110. _next C@ IF _next C@ 0 _next C! 1 EXIT THEN
  111. _updsel IF
  112. _prevstat C@
  113. 0x20 ( BUTC ) OVER AND NOT IF DROP _sel C@ 1 EXIT THEN
  114. 0x40 ( BUTA ) AND NOT IF 0x8 ( BS ) 1 EXIT THEN
  115. ( If not BUTC or BUTA, it has to be START )
  116. 0xd _next C! _sel C@ 1
  117. ELSE 0 ( f ) THEN ;
  118. ( ----- 617 )
  119. : PAD$
  120. 0xff _prevstat C! 'a' _sel C! 0 _next C! ;
  121. ( ----- 620 )
  122. ( kbd - implement (ps2kc) for SMS PS/2 adapter )
  123. : (ps2kcA) ( for port A )
  124. ( Before reading a character, we must first verify that there
  125. is something to read. When the adapter is finished filling its
  126. '164 up, it resets the latch, which output's is connected to
  127. TL. When the '164 is full, TL is low. Port A TL is bit 4 )
  128. _D1@ 0x10 AND IF 0 EXIT ( nothing ) THEN
  129. 0 _THA! ( Port A TH output, low )
  130. _D1@ ( bit 3:0 go in 3:0 ) 0x0f AND ( n )
  131. 1 _THA! ( Port A TH output, high )
  132. _D1@ ( bit 3:0 go in 7:4 ) 0x0f AND 4 LSHIFT OR ( n )
  133. 2 _THA! ( TH input ) ;
  134. ( ----- 621 )
  135. : (ps2kcB) ( for port B )
  136. ( Port B TL is bit 2 )
  137. _D2@ 0x04 AND IF 0 EXIT ( nothing ) THEN
  138. 0 _THB! ( Port B TH output, low )
  139. _D1@ ( bit 7:6 go in 1:0 ) 6 RSHIFT ( n )
  140. _D2@ ( bit 1:0 go in 3:2 ) 0x03 AND 2 LSHIFT OR ( n )
  141. 1 _THB! ( Port B TH output, high )
  142. _D1@ ( bit 7:6 go in 5:4 ) 0xc0 AND 2 RSHIFT OR ( n )
  143. _D2@ ( bit 1:0 go in 7:6 ) 0x03 AND 6 LSHIFT OR ( n )
  144. 2 _THB! ( TH input ) ;
  145. ( ----- 622 )
  146. : (spie) DROP ; ( always enabled )
  147. CODE (spix) ( x -- x, for port B ) HL POP, chkPS,
  148. ( TR = DATA TH = CLK )
  149. CPORT_MEM LDA(i), 0xf3 ANDi, ( TR/TH output )
  150. H 8 LDri, BEGIN,
  151. 0xbf ANDi, ( TR lo ) L RL, ( --> C )
  152. IFC, 0x40 ORi, ( TR hi ) THEN,
  153. CPORT_CTL OUTiA, ( clic! ) 0x80 ORi, ( TH hi )
  154. CPORT_CTL OUTiA, ( clac! )
  155. EXAFAF', CPORT_D1 INAi, ( Up Btn is B6 ) RLA, RLA,
  156. E RL, EXAFAF',
  157. 0x7f ANDi, ( TH lo ) CPORT_CTL OUTiA, ( cloc! )
  158. H DECr, JRNZ, AGAIN, CPORT_MEM LD(i)A,
  159. L E LDrr, HL PUSH,
  160. ;CODE
  161. ( ----- 625 )
  162. ( Routines for interacting with SMS controller ports.
  163. Requires CPORT_MEM, CPORT_CTL, CPORT_D1 and CPORT_D2 to be
  164. defined. CPORT_MEM is a 1 byte buffer for CPORT_CTL. The last
  165. 3 consts will usually be 0x3f, 0xdc, 0xdd. )
  166. ( mode -- set TR pin on mode a on:
  167. 0= output low 1=output high 2=input )
  168. CODE _TRA! HL POP, chkPS, ( B0 -> B4, B1 -> B0 )
  169. L RR, RLA, RLA, RLA, RLA, L RR, RLA,
  170. 0x11 ANDi, L A LDrr, CPORT_MEM LDA(i),
  171. 0xee ANDi, L ORr, CPORT_CTL OUTiA, CPORT_MEM LD(i)A,
  172. ;CODE
  173. CODE _THA! HL POP, chkPS, ( B0 -> B5, B1 -> B1 )
  174. L RR, RLA, RLA, RLA, RLA, L RR, RLA, RLA,
  175. 0x22 ANDi, L A LDrr, CPORT_MEM LDA(i),
  176. 0xdd ANDi, L ORr, CPORT_CTL OUTiA, CPORT_MEM LD(i)A,
  177. ;CODE
  178. ( ----- 626 )
  179. CODE _TRB! HL POP, chkPS, ( B0 -> B6, B1 -> B2 )
  180. L RR, RLA, RLA, RLA, RLA, L RR, RLA, RLA, RLA,
  181. 0x44 ANDi, L A LDrr, CPORT_MEM LDA(i),
  182. 0xbb ANDi, L ORr, CPORT_CTL OUTiA, CPORT_MEM LD(i)A,
  183. ;CODE
  184. CODE _THB! HL POP, chkPS, ( B0 -> B7, B1 -> B3 )
  185. L RR, RLA, RLA, RLA, RLA, L RR, RLA, RLA, RLA, RLA,
  186. 0x88 ANDi, L A LDrr, CPORT_MEM LDA(i),
  187. 0x77 ANDi, L ORr, CPORT_CTL OUTiA, CPORT_MEM LD(i)A,
  188. ;CODE
  189. CODE _D1@ CPORT_D1 INAi, PUSHA, ;CODE
  190. CODE _D2@ CPORT_D2 INAi, PUSHA, ;CODE