graphics.asm 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. ; $Id$
  2. ; MegaZeux
  3. ;
  4. ; Copyright (C) 1996 Greg Janson
  5. ; Copyright (C) 1998 Matthew D. Williams - dbwilli@scsn.net
  6. ;
  7. ; This program is free software; you can redistribute it and/or
  8. ; modify it under the terms of the GNU General Public License as
  9. ; published by the Free Software Foundation; either version 2 of
  10. ; the License, or (at your option) any later version.
  11. ;
  12. ; This program is distributed in the hope that it will be useful,
  13. ; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ; General Public License for more details.
  16. ;
  17. ; You should have received a copy of the GNU General Public License
  18. ; along with this program; if not, write to the Free Software
  19. ; Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. ;
  21. ; GRAPHICS.ASM- Simple text graphics code
  22. ;
  23. Ideal
  24. include "graphics.inc"
  25. p186
  26. JUMPS
  27. include "model.inc"
  28. Codeseg
  29. ;
  30. ; Function- color_string
  31. ;
  32. ; Reasonably optimized. (186)
  33. ;
  34. ; Code to output a string direct to video memory, interpreting \n (0x0A) as
  35. ; a carraige return + linefeed combo, ~ as an "attribute" setting
  36. ; pre-code (follow with 0-F to set foreground), @ as a "background"
  37. ; setting pre-code (follow with 0-F to set background), and \0 (0x00) as an
  38. ; EOS (end-of-string) code. Carraige returns move down one line (without
  39. ; scrolling) and reset the x position to it's value upon entering
  40. ; color_string. Reaching the last space onscreen returns to the start of
  41. ; that line. If ~ or @ is followed by a \0 it counts as the end of the
  42. ; string, and the ~ or @ is ignored. If ~ or @ is followed by anything
  43. ; else, the first ~ or @ is ignored and the second character is printed
  44. ; as normal. This allows imbedding of actual ~ and @ signs.
  45. ;
  46. ; A tab (0x05) will NOT SKIP spaces.
  47. ;
  48. ; Arguments: far pointer to string (byte array)
  49. ; x position (word)
  50. ; y position (word)
  51. ; default color (byte)
  52. ; video segment (word)
  53. ;
  54. proc color_string far
  55. arg string_offs:word,string_segm:word,x_start:word,y_start:word,color:byte,segm:word
  56. push ax bx cx dx es si di ds ; Conserve registers
  57. mov ax,[segm] ; Load in segment of video memory
  58. mov es,ax
  59. mov bx,[x_start] ; Load current x/y positions
  60. mov cx,[y_start]
  61. mov si,[string_offs] ; Offset into string
  62. mov ax,[string_segm]
  63. mov ds,ax ; Load in segment of string
  64. mov dl,[color] ; Load default color
  65. cld ; Move increasing in string instructions
  66. ; Contents/usage of registers-
  67. ; DS:SI = string, ES:DI = video, DL = color,
  68. ; BX = X position, CX = Y position, AL = temp/char
  69. @@recalc_loop:
  70. ; The following code multiplies current y position by 80, adds
  71. ; x position, doubles the total, and stores in di (IE recalculates
  72. ; the current offset)
  73. mov di,cx
  74. shl di,2
  75. add di,cx
  76. shl di,4
  77. add di,bx
  78. add di,di
  79. @@main_loop:
  80. lodsb ; Read next byte of string and update
  81. cmp al,0Ah ; Check for return
  82. je @@return ; Carraige return + line feed
  83. cmp al,'~' ; Check for ~
  84. je @@attrib ; Attribute code
  85. cmp al,'@' ; Check for @
  86. je @@backgr ; Background code
  87. cmp al,00h ; Check for EOS
  88. je @@done ; Done with string
  89. @@printchr:
  90. ; Print character in al with color in dl
  91. mov [es:di],al ; Output character
  92. mov [es:di+1],dl ; Output color
  93. @@nextspot:
  94. ; Update cursor position
  95. inc bx ; Increase x position
  96. cmp bx,80 ; Compare to # of columns (are we offscreen?)
  97. je @@return ; Do a carraige return
  98. add di,2 ; Otherwise, fix offset into vid mem and..
  99. jmp @@main_loop ; loop.
  100. @@return:
  101. ; Reset X position to starting position, Y position to next lower or same
  102. ; if at lowest position already.
  103. mov bx,[x_start] ; Load starting x position
  104. cmp cx,24 ; Last line?
  105. je @@recalc_loop ; Yep.
  106. inc cx ; Nope, move to next line
  107. jmp @@recalc_loop ; Go and recalc
  108. @@attrib:
  109. ; Read next byte for foreground color (0-9, a-f, A-F)
  110. lodsb ; Read next byte of string and update
  111. cmp al,0 ; EOS?
  112. je @@done
  113. cmp al,'0'
  114. jae @@a_a0
  115. jmp @@printchr ; Illegal attrib char
  116. @@a_a0:
  117. cmp al,'A'
  118. jae @@a_aa
  119. cmp al,'9'
  120. ja @@printchr ; Illegal attrib char
  121. ; al is '0' through '9'
  122. sub al,48 ; Translate to 0-9
  123. jmp @@aset ; Now set attrib
  124. @@a_aa:
  125. cmp al,'a'
  126. jae @@a_aua
  127. cmp al,'F'
  128. ja @@printchr ; Illegal attrib char
  129. ; al is 'A' through 'F'
  130. sub al,55 ; Translate to 0-9
  131. jmp @@aset ; Now set attrib
  132. @@a_aua:
  133. cmp al,'f'
  134. ja @@printchr ; Illegal attrib char
  135. ; al is 'a' through 'f'
  136. sub al,87 ; Translate to 0-9
  137. @@aset:
  138. ; Set foreground attrib to al
  139. maskflag dl,0f0h ; Mask out old foreground
  140. add dl,al ; Add in new foreground
  141. jmp @@main_loop ; Loop
  142. @@backgr:
  143. ; Read next byte for background color (0-9, a-f, A-F)
  144. lodsb ; Read next byte of string and update
  145. cmp al,0 ; EOS?
  146. je @@done
  147. cmp al,'0'
  148. jae @@b_a0
  149. jmp @@printchr ; Illegal attrib char
  150. @@b_a0:
  151. cmp al,'A'
  152. jae @@b_aa
  153. cmp al,'9'
  154. ja @@printchr ; Illegal attrib char
  155. ; al is '0' through '9'
  156. sub al,48 ; Translate to 0-9
  157. jmp @@bset ; Now set attrib
  158. @@b_aa:
  159. cmp al,'a'
  160. jae @@b_aua
  161. cmp al,'F'
  162. ja @@printchr ; Illegal attrib char
  163. ; al is 'A' through 'F'
  164. sub al,55 ; Translate to 0-9
  165. jmp @@bset ; Now set attrib
  166. @@b_aua:
  167. cmp al,'f'
  168. ja @@printchr ; Illegal attrib char
  169. ; al is 'a' through 'f'
  170. sub al,87 ; Translate to 0-9
  171. @@bset:
  172. ; Set background attrib to al
  173. maskflag dl,00fh ; Mask out old background
  174. sal al,4 ; Multiply new background by 16
  175. add dl,al ; Add it in
  176. jmp @@main_loop ; Loop
  177. @@done:
  178. pop ds di si es dx cx bx ax ; Restore registers
  179. ret
  180. endp color_string
  181. ;
  182. ; Function- write_string
  183. ;
  184. ; Reasonably optimized (186)
  185. ;
  186. ; Code to output a string direct to video memory, interpreting \n (0x0A) as
  187. ; a carraige return + linefeed combo and \0 (0x00) as an EOS (end-of-string)
  188. ; code. Carraige returns move down one line (without scrolling) and reset the
  189. ; x position to it's value upon entering write_string. Reaching the last
  190. ; space onscreen exits the routine.
  191. ;
  192. ; A tab (0x05) will skip five spaces.
  193. ;
  194. ; Arguments: far pointer to string (byte array)
  195. ; x position (word)
  196. ; y position (word)
  197. ; color (byte)
  198. ; video segment (word)
  199. ;
  200. proc write_string far
  201. arg string_offs:word,string_segm:word,x_start:word,y_start:word,color:byte,segm:word,taballowed:byte
  202. push ax bx cx es si di ds ; Conserve registers
  203. cmp [x_start],79
  204. ja @@done
  205. mov bx,[segm] ; Load in segment of video memory
  206. mov es,bx
  207. mov si,[string_offs] ; Offset into string
  208. mov bx,[string_segm]
  209. mov ds,bx ; Load in segment of string
  210. mov bx,[x_start] ; Load current x/y positions
  211. mov cx,[y_start]
  212. mov ah,[color] ; Read color
  213. cld ; Move increasing in string instructions
  214. @@recalc_loop:
  215. ; Set di to video offset
  216. mov di,cx
  217. shl di,2
  218. add di,cx
  219. shl di,4
  220. add di,bx
  221. add di,di
  222. ;bx=current x, cx=current y
  223. ;es:di=current video, ds:si=current string
  224. ;al=char, ah=color
  225. @@main_loop:
  226. lodsb ; Read next byte of string and update
  227. cmp al,0Ah ; Check for return
  228. je @@return ; Carraige return + line feed
  229. cmp al,5 ; Tab
  230. je @@tab
  231. cmp al,00h ; Check for EOS
  232. je @@done ; Done with string
  233. @@printchr:
  234. ; Print character in al with color in ah
  235. stosw
  236. @@nextspot:
  237. ; Update cursor position
  238. inc bx ; Increase x position
  239. cmp bx,80 ; Compare to # of columns (are we offscreen?)
  240. jne @@main_loop ; Nope, loop
  241. jmp @@return ; Yep, do a carraige return
  242. @@tab:
  243. ; SKIP five spaces
  244. cmp [taballowed],1
  245. jne @@printchr
  246. add bx,5
  247. add di,10
  248. cmp bx,80 ; Too far?
  249. jb @@main_loop ; No, loop. Yes, fall thru to return.
  250. @@return:
  251. ; Reset X position to starting position, Y position to next lower or same
  252. ; if at lowest position already.
  253. mov bx,[x_start] ; Load starting x position
  254. inc cx ; Increase y position
  255. cmp cx,25 ; Compare to 25 (offscreen)
  256. jne @@recalc_loop ; Nope, loop
  257. ; Yep, done
  258. @@done:
  259. pop ds di si es cx bx ax ; Restore registers
  260. ret
  261. endp write_string
  262. ;
  263. ; Function- write_line
  264. ;
  265. ; Reasonably optimized. (186)
  266. ;
  267. ; Code to output a string direct to video memory, interpreting \n (0x0A) or
  268. ; \0 (0x00) as an EOS (end-of-string) code. Reaching end of line ends the
  269. ; routine.
  270. ;
  271. ; A tab (0x05) will skip five spaces.
  272. ;
  273. ; A null (0x00) is displayed as a character.
  274. ;
  275. ; Arguments: far pointer to string (byte array)
  276. ; x position (word)
  277. ; y position (word)
  278. ; color (byte)
  279. ; video segment (word)
  280. ;
  281. proc write_line far
  282. arg string_offs:word,string_segm:word,x_start:word,y_start:word,color:byte,segm:word,taballowed:byte
  283. push ax bx cx es si di ds ; Conserve registers
  284. cmp [x_start],79
  285. ja @@done
  286. mov bx,[segm] ; Load in segment of video memory
  287. mov es,bx
  288. mov si,[string_offs] ; Offset into string
  289. mov bx,[string_segm]
  290. mov ds,bx ; Load in segment of string
  291. mov bx,[x_start] ; Load current x/y positions
  292. mov cx,[y_start]
  293. mov ah,[color] ; Read color
  294. cld ; Move increasing in string instructions
  295. ; Set di to video offset
  296. mov di,cx
  297. shl di,2
  298. add di,cx
  299. shl di,4
  300. add di,bx
  301. add di,di
  302. ;bx=current x, cx=current y
  303. ;es:di=current video, ds:si=current string
  304. ;al=char, ah=color
  305. @@main_loop:
  306. lodsb ; Read next byte of string and update
  307. cmp al,0Ah ; Check for return
  308. je @@done ; Done with string
  309. cmp al,0
  310. je @@done
  311. cmp al,5 ; Tab
  312. je @@tab
  313. @@printchr:
  314. ; Print character in al with color in ah
  315. stosw
  316. @@nextspot:
  317. ; Update cursor position
  318. inc bx ; Increase x position
  319. cmp bx,80 ; Compare to # of columns (are we offscreen?)
  320. jne @@main_loop ; Nope, loop
  321. jmp @@done ; Yep, done
  322. @@tab:
  323. ; SKIP five spaces
  324. cmp [taballowed],1
  325. jne @@printchr
  326. add bx,5
  327. add di,10
  328. cmp bx,80 ; Too far?
  329. jb @@main_loop ; No, loop. Yes, fall thru to done.
  330. @@done:
  331. pop ds di si es cx bx ax ; Restore registers
  332. ret
  333. endp write_line
  334. ;
  335. ; Function- color_line
  336. ;
  337. ; Reasonably optimized. (186)
  338. ;
  339. ; Code to fill a given x/y with color for length
  340. ;
  341. ; Arguments: length (word)
  342. ; x position (word)
  343. ; y position (word)
  344. ; color (byte)
  345. ; video segment (word)
  346. ;
  347. proc color_line far
  348. arg len:word,x_start:word,y_start:word,color:byte,segm:word
  349. push ax di cx es ; Conserve registers
  350. mov ax,[segm] ; Load in segment of video memory
  351. mov es,ax
  352. mov ax,[x_start] ; Load current x/y positions
  353. mov cx,[y_start]
  354. ;
  355. ; Calculate offset within screen
  356. ;
  357. mov di,cx
  358. shl di,2
  359. add di,cx
  360. shl di,4
  361. add di,ax
  362. add di,di
  363. inc di ; Point at COLOR
  364. mov cx,[len]
  365. mov al,[color] ; Read color
  366. @@col_loop:
  367. mov [es:di],al ; Output color
  368. add di,2 ; Next color position
  369. loop @@col_loop ; Loop for CX (length)
  370. pop es cx di ax ; Restore registers
  371. ret
  372. endp color_line
  373. ;
  374. ; Function- fill_line
  375. ;
  376. ; Reasonably optimized. (186)
  377. ;
  378. ; Code to fill a given x/y with color and character for length
  379. ;
  380. ; Arguments: length (word)
  381. ; x position (word)
  382. ; y position (word)
  383. ; char/colr combo (word)
  384. ; video segment (word)
  385. ;
  386. proc fill_line far
  387. arg len:word,x_start:word,y_start:word,char_col:word,segm:word
  388. push ax di cx es ; Conserve registers
  389. mov ax,[segm] ; Load in segment of video memory
  390. mov es,ax
  391. mov ax,[x_start] ; Load current x/y positions
  392. mov cx,[y_start]
  393. ;
  394. ; Calculate offset within screen
  395. ;
  396. mov di,cx
  397. shl di,2
  398. add di,cx
  399. shl di,4
  400. add di,ax
  401. add di,di
  402. mov cx,[len]
  403. mov ax,[char_col] ; Read char/col
  404. cld
  405. rep stosw
  406. pop es cx di ax ; Restore registers
  407. ret
  408. endp fill_line
  409. ;
  410. ; Function- draw_char
  411. ;
  412. ; Reasonably optimized. (186)
  413. ;
  414. ; Code to output a single character direct to video memory.
  415. ;
  416. ; Arguments: character (word)
  417. ; color (word)
  418. ; x position (word)
  419. ; y position (word)
  420. ; video segment (word)
  421. ;
  422. proc draw_char far
  423. arg chr:word,color:word,x_pos:word,y_pos:word,segm:word
  424. push ax bx es di ; Conserve registers
  425. cmp [x_pos],79
  426. ja @@done
  427. mov ax,[segm] ; Load in segment of video memory
  428. mov es,ax
  429. mov bx,[y_pos]
  430. mov di,bx
  431. shl di,2
  432. add di,bx
  433. shl di,4
  434. add di,[x_pos]
  435. add di,di
  436. mov bx,[color] ; Read color
  437. mov ax,[chr]
  438. mov [es:di],al ; Output character
  439. mov [es:di+1],bl ; Output color
  440. @@done:
  441. pop di es bx ax ; Restore registers
  442. ret
  443. endp draw_char
  444. proc draw_char_linear far
  445. arg chr:byte,color:byte,pos:word,segm:word
  446. push ax bx es di ; Conserve registers
  447. push ax
  448. push bx
  449. push es
  450. push di ; Conserve registers
  451. mov ax,[segm] ; Load in segment of video memory
  452. mov es,ax
  453. mov di, [pos] ; position
  454. shl di, 1
  455. mov al, [color] ; color
  456. mov ah, [chr] ; char
  457. mov [es:di],ax ; Output character/color
  458. @@done:
  459. pop di es bx ax ; Restore registers
  460. ret
  461. endp draw_char_linear
  462. ;
  463. ; Function- page_flip
  464. ;
  465. ; Optimization note- Interrupt casing. (186)
  466. ;
  467. ; Flips to a different text page. (0 through 3 on EGA text) Does not do any
  468. ; vertical retrace waiting.
  469. ;
  470. ; Arguments: page (char)
  471. ;
  472. proc page_flip far
  473. arg pagenum:word
  474. pusha
  475. mov ax,[pagenum]
  476. mov ah,5
  477. int 10h
  478. popa
  479. ret
  480. endp page_flip
  481. ;
  482. ; Function- clear_screen
  483. ;
  484. ; Fills the screen with any char/color combo.
  485. ;
  486. ; Arguments: char/color combo (word)
  487. ; video segment (word)
  488. proc clear_screen
  489. arg c_c_c:word,segm:word
  490. push es di ax cx
  491. mov di,[segm]
  492. mov es,di
  493. xor di,di
  494. mov ax,[c_c_c]
  495. mov cx,2000
  496. cld
  497. rep stosw
  498. pop cx ax di es
  499. ret
  500. endp
  501. ends
  502. end