iffroutine.s 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. *****************************************************************
  2. * MODULE TITLE :iffroutine *
  3. * *
  4. * DESCRIPTION :iff loader and iff saver *
  5. * *
  6. * *
  7. * *
  8. * NAME DATE *
  9. * *
  10. * LIST OF ROUTINES :load_graphics *
  11. * insert_byte *
  12. * dealloc_pic_mem *
  13. * find_in_mem *
  14. * setup_plane_offsets *
  15. * save_graphics *
  16. * setup_bmhd *
  17. * save_pic *
  18. * *
  19. *****************************************************************
  20. MEM_CHIP EQU $02
  21. MEM_FAST EQU $04
  22. MEM_CLEAR EQU $10000
  23. ********************************
  24. **** LOAD GRAPHICS *****
  25. ********************************
  26. load_graphics
  27. move.b #1,pointer_state
  28. move.l a6,-(sp)
  29. *send in file handle in d0
  30. *returns pointer to struct containing pic mem pointers in a0
  31. move.l d0,graphics_handle
  32. move.l d0,d1
  33. move.l #buffer,d2
  34. move.l #4,d3
  35. move.l dosbase,a5
  36. jsr READ(a5)
  37. cmp.l #'FORM',buffer
  38. beq.s ok_so_far
  39. move.w #1000,d0
  40. bra exit_with_error
  41. ok_so_far
  42. move.l graphics_handle,d1 ;read size of file
  43. move.l #buffer,d2
  44. move.l #4,d3
  45. move.l dosbase,a6
  46. jsr READ(a6)
  47. move.l 4,a6 ; exec
  48. move.l buffer,d0 ; mem to allocate
  49. move.l #MEM_FAST+MEM_CLEAR,d1 ; chip and clear
  50. jsr -198(a6) ; try
  51. tst.l d0
  52. bne alloc_pic_size
  53. move.w #1001,d0
  54. bra exit_with_error
  55. alloc_pic_size
  56. move.l d0,pic_pointer
  57. move.l d0,d2
  58. move.l buffer,d3
  59. move.l graphics_handle,d1
  60. move.l dosbase,a6
  61. jsr READ(a6) ;read in all data
  62. move.l pic_pointer,a1
  63. move.l #'BMHD',d0
  64. bsr find_in_mem
  65. move.l (a1)+,header_size
  66. move.l a1,picture_details
  67. move.l #'CMAP',d0
  68. bsr find_in_mem
  69. move.l (a1)+,num_of_cols
  70. move.l a1,colour_map_ptr
  71. move.l page_pointer,a3
  72. move.l screen_palette(a3),a0
  73. bsr insert_cols
  74. move.l #'BODY',d0
  75. bsr find_in_mem
  76. move.l (a1)+,size_of_pic
  77. move.l a1,picture_data ;at last we are here
  78. move.l picture_details,a0
  79. move.w RWIDTH(a0),screen_x_size(a3)
  80. move.w RHEIGHT(a0),screen_y_size(a3)
  81. moveq #0,d0
  82. move.b NUMPLANES(a0),d0
  83. move.w d0,number_of_planes(a3)
  84. bsr clear_old_data
  85. bsr setup_plane_offsets
  86. move.l picture_data,a1
  87. move.l picture_details,a0
  88. cmp.b #0,TYPECOM(a0)
  89. beq.s call_uncom
  90. bsr compressed_data
  91. bra.s finished_uncom
  92. call_uncom
  93. bsr uncompressed_data
  94. finished_uncom
  95. bsr dealloc_pic_mem
  96. bra.s quit_ld_grap
  97. exit_with_error
  98. move.l #-1,a1
  99. quit_ld_grap
  100. move.b #0,pointer_state
  101. move.l (sp)+,a6
  102. move.l page_pointer,a0
  103. move.l screen_palette(a0),a1
  104. bsr setup_screen_colours
  105. bsr display_graphic_page ;display picture
  106. rts
  107. compressed_data
  108. bsr loadabit
  109. move.b d0,d1
  110. tst.b d1 ;loop
  111. bmi repeat_data
  112. addq.b #1,d1
  113. bra.s read_and_insert
  114. loop_read
  115. cmp.l #0,size_of_pic
  116. bgt.s compressed_data
  117. rts
  118. read_and_insert
  119. bsr loadabit
  120. subq.b #1,d1
  121. bsr insert_byte
  122. cmp.b #0,d1
  123. bne.s read_and_insert
  124. bra loop_read
  125. repeat_data
  126. cmp.b #-128,d1
  127. bne.s do_something
  128. subq.l #1,size_of_pic
  129. bra loop_read
  130. do_something
  131. neg.b d1
  132. addq.b #1,d1
  133. bsr loadabit
  134. repeat_loop
  135. bsr insert_byte
  136. subq.b #1,d1
  137. bne.s repeat_loop
  138. bra.s loop_read
  139. uncompressed_data
  140. bsr loadabit
  141. bsr insert_byte
  142. cmp.l #0,size_of_pic
  143. bgt.s uncompressed_data
  144. rts
  145. ********************************
  146. **** INSERT BYTE *****
  147. ********************************
  148. insert_byte
  149. move.l page_pointer,a0
  150. move.l screen_mem(a0),a0
  151. moveq #0,d2
  152. moveq #0,d3
  153. move.w current_plane,d3
  154. move.l #plane_positions,a2
  155. asl.w #2,d3
  156. move.l (a2,d3.l),d4
  157. move.b d0,(a0,d4.l)
  158. move.l #main_screen_struct,a5
  159. addq.l #1,(a2,d3.l)
  160. addq.w #8,pixel_count
  161. move.w screen_x_size(a5),d3
  162. cmp.w pixel_count,d3
  163. bne.s not_done_line
  164. bra.s update_planes
  165. not_done_line
  166. move.l page_pointer,a5
  167. move.w RWIDTH(a5),d2
  168. add.w #15,d2
  169. andi.w #$fff0,d2
  170. cmp.w pixel_count,d2
  171. bne.s not_done_yet
  172. moveq #0,d2
  173. move.w #320,d2 ;naughty naughty
  174. sub.w pixel_count,d2
  175. asr.w #3,d2
  176. move.w current_plane,d3
  177. asl.w #2,d3
  178. add.l d2,(a2,d3.l) move onto next line
  179. update_planes
  180. move.w #0,pixel_count
  181. addq.w #1,current_plane
  182. move.l page_pointer,a5
  183. move.w number_of_planes(a5),d3
  184. cmp.w current_plane,d3
  185. bne.s not_done_line
  186. move.w #0,current_plane
  187. not_done_yet
  188. rts
  189. loadabit
  190. moveq #0,d0
  191. move.b (a1)+,d0 ;pointer to pic data
  192. sub.l #1,size_of_pic
  193. rts
  194. ********************************
  195. **** DEALLOC PIC MEM *****
  196. ********************************
  197. dealloc_pic_mem
  198. move.l 4,a6 ; deallocate mem
  199. move.l buffer,d0
  200. move.l pic_pointer,a1
  201. jsr -210(a6)
  202. rts
  203. ********************************
  204. **** ALLOCATE SCREEN MEM *****
  205. ********************************
  206. allocate_screen_mem
  207. ****PLEASE NOTE - bodgy code is about
  208. ****THE picture structs allocate the same size as the screen
  209. ****but the x and y size contained in the struct are that of the
  210. ****graphic image
  211. movem.l a0-a6/d0-d7,-(sp)
  212. *** SEE IF NEED TO DEALLOCATE OLD PICTURE
  213. move.l page_pointer,a0
  214. tst.l screen_mem(a0)
  215. beq.s no_need_to_deallocate
  216. move.l #main_screen_struct,a4
  217. move.w screen_x_size(a4),d0
  218. asr.w #3,d0
  219. mulu screen_y_size(a4),d0
  220. mulu number_of_planes(a0),d0
  221. move.l screen_mem(a0),a1
  222. move.l 4,a6
  223. jsr -210(a6)
  224. no_need_to_deallocate
  225. *****NOW TRY AND ALLOCATE NEW ONE
  226. move.l #main_screen_struct,a0
  227. move.w screen_x_size(a0),d0
  228. asr.w #3,d0
  229. mulu screen_y_size(a0),d0
  230. move.l page_pointer,a0
  231. mulu number_of_planes(a0),d0
  232. move.l 4,a6
  233. move.l #MEM_FAST+MEM_CLEAR,d1
  234. jsr -198(a6)
  235. tst.l d0
  236. bne.s got_the_mem
  237. move.l #main_screen_colours+2,a2
  238. move.w #$fff,(a2)
  239. got_the_mem
  240. move.l page_pointer,a0
  241. move.l d0,screen_mem(a0)
  242. movem.l (sp)+,a0-a6/d0-d7
  243. rts
  244. ********************************
  245. **** CLEAR_OLD_DATA *****
  246. ********************************
  247. clear_old_data
  248. movem.l a0/d0,-(sp)
  249. move.l #main_screen_struct,a0
  250. move.w screen_x_size(a0),d0
  251. asr.w #3,d0
  252. mulu screen_y_size(a0),d0
  253. mulu number_of_planes(a0),d0
  254. asr.l #2,d0
  255. move.l page_pointer,a0
  256. move.l screen_mem(a0),a0
  257. clear_old_data_loop
  258. clr.l (a0)+
  259. subq.l #1,d0
  260. bne.s clear_old_data_loop
  261. movem.l (sp)+,a0/d0
  262. ********************************
  263. **** FIND IN MEM *****
  264. ********************************
  265. find_in_mem
  266. move.l a1,a0
  267. set_up_loop
  268. move.w #3,d2
  269. moveq #0,d1
  270. test_in_loop
  271. rol.l #8,d1
  272. or.b (a0)+,d1
  273. dbra d2,test_in_loop
  274. cmp.l d1,d0
  275. beq.s found_the_text
  276. add.l #1,a1
  277. bra.s find_in_mem
  278. found_the_text
  279. add.l #4,a1 ;get past text
  280. rts
  281. ********************************
  282. **** SETUP PLANE OFFSETS *****
  283. ********************************
  284. setup_plane_offsets
  285. move.l #main_screen_struct,a0
  286. moveq #0,d0
  287. move.w screen_x_size(a0),d0
  288. asr.w #3,d0
  289. mulu screen_y_size(a0),d0
  290. move.w number_of_planes(a0),d1
  291. subq.w #1,d1
  292. moveq #0,d2
  293. move.l #plane_positions,a0
  294. calculate_offsets
  295. move.l d2,(a0)+
  296. add.l d0,d2
  297. dbra d1,calculate_offsets
  298. rts
  299. ********************************
  300. **** SAVE GRAPHICS *****
  301. ********************************
  302. save_graphics
  303. move.b #1,pointer_state
  304. bsr remove_file_request
  305. movem.l a6,-(sp)
  306. move.l dosbase,a6
  307. move.l #current_filename,d1
  308. move.l #MODE_NEW,d2
  309. jsr OPEN(a6)
  310. tst.l d0
  311. bne.s go_and_save_pic
  312. movem.l (sp)+,a6
  313. bsr display_error
  314. bra exit_save_pic
  315. go_and_save_pic
  316. move.l d0,save_file_handle
  317. ***write out header
  318. move.l #"FORM",save_buffer
  319. move.l d0,d1
  320. move.l #save_buffer,d2
  321. move.l #4,d3
  322. jsr WRITE(a6)
  323. ***calculate size of file
  324. move.l #main_screen_struct,a0
  325. move.w screen_x_size(a0),d0
  326. asr.w #3,d0
  327. mulu screen_y_size(a0),d0
  328. mulu number_of_planes(a0),d0 ;size of pic
  329. add.l #4+4+4+4+4+4+4,d0 ;BMHD,CMAP,BODY text, BMHDSIZE and BODY SIZE + number of colours
  330. add.l #size_of_bmhd,d0
  331. add.l #3*16,d0 ;colours (16)
  332. move.l d0,save_buffer
  333. move.l save_file_handle,d1
  334. move.l #save_buffer,d2
  335. move.l #4,d3
  336. jsr WRITE(a6)
  337. ****write bmhd
  338. move.l #"ILBM",save_buffer
  339. move.l save_file_handle,d1
  340. move.l #save_buffer,d2
  341. move.l #4,d3
  342. jsr WRITE(a6)
  343. move.l #"BMHD",save_buffer
  344. move.l save_file_handle,d1
  345. move.l #save_buffer,d2
  346. move.l #4,d3
  347. jsr WRITE(a6)
  348. move.l #size_of_bmhd,save_buffer
  349. move.l save_file_handle,d1
  350. move.l #save_buffer,d2
  351. move.l #4,d3
  352. jsr WRITE(a6)
  353. bsr setup_bmhd
  354. move.l save_file_handle,d1
  355. move.l #save_bmhd,d2
  356. move.l #size_of_bmhd,d3
  357. jsr WRITE(a6)
  358. ****do colourmap
  359. move.l #"CMAP",save_buffer
  360. move.l save_file_handle,d1
  361. move.l #save_buffer,d2
  362. move.l #4,d3
  363. jsr WRITE(a6)
  364. move.l #16*3,save_buffer ;number of colours
  365. move.l save_file_handle,d1
  366. move.l #save_buffer,d2
  367. move.l #4,d3
  368. jsr WRITE(a6)
  369. move.l save_file_handle,d1
  370. move.l #Default_Colours,d2
  371. move.l #16*3,d3
  372. jsr WRITE(a6)
  373. ****do body
  374. move.l #"BODY",save_buffer
  375. move.l save_file_handle,d1
  376. move.l #save_buffer,d2
  377. move.l #4,d3
  378. jsr WRITE(a6)
  379. move.l #main_screen_struct,a0
  380. move.w screen_x_size(a0),d0
  381. asr.w #3,d0
  382. mulu screen_y_size(a0),d0
  383. mulu number_of_planes(a0),d0
  384. move.l d0,save_buffer
  385. move.l save_file_handle,d1
  386. move.l #save_buffer,d2
  387. move.l #4,d3
  388. jsr WRITE(a6)
  389. ****write out body data
  390. move.l #main_screen_struct,a0
  391. move.w screen_y_size(a0),d0
  392. moveq #0,d1
  393. move.w screen_x_size(a0),d1
  394. asr.w #3,d1
  395. move.l screen_mem(a0),a1
  396. subq.w #1,d0
  397. write_pic_loop
  398. move.l a1,a2
  399. move.w number_of_planes(a0),d2
  400. subq.w #1,d2
  401. write_plane_pic_loop
  402. movem.l a0-a2/d0-d2,-(sp) ;write line
  403. move.l d1,d3 ;size
  404. move.l save_file_handle,d1 ;handle
  405. move.l a2,d2 ;place to get from
  406. jsr write(a6)
  407. movem.l (sp)+,a0-a2/d0-d2
  408. move.l d1,d4
  409. mulu screen_y_size(a0),d4 ;plane size
  410. add.l d4,a2
  411. dbra d2,write_plane_pic_loop
  412. add.l d1,a1
  413. dbra d0,write_pic_loop
  414. move.l save_file_handle,d1
  415. jsr CLOSE(a6)
  416. quit_save_pic
  417. movem.l (sp)+,a6
  418. exit_save_pic
  419. move.b #0,pointer_state
  420. rts
  421. ********************************
  422. **** SETUP BMHD *****
  423. ********************************
  424. setup_bmhd
  425. move.l #main_screen_struct,a0
  426. move.l #save_bmhd,a1
  427. move.w screen_x_size(a0),RWIDTH(a1)
  428. move.w screen_y_size(a0),RHEIGHT(a1)
  429. move.w #0,XPOS(a1)
  430. move.w #0,YPOS(a1)
  431. move.w number_of_planes(a0),d0
  432. move.b d0,numplanes(a1)
  433. move.b #0,PMASK(a1)
  434. move.b #0,TYPECOM(a1)
  435. move.b #0,PAD(a1)
  436. move.w #0,TRANSCOLOUR(a1)
  437. move.b #0,XASPECT(a1)
  438. move.b #0,YASPECT(a1)
  439. move.w #0,PAGEWIDTH(a1)
  440. move.w #0,PAGEHEIGHT(a1)
  441. move.b #16*3,ALLOCCOLS(a1)
  442. rts
  443. ********************************
  444. **** SAVE PIC *****
  445. ********************************
  446. save_pic
  447. move.l #save_graphics,file_routine_pointer
  448. bsr display_file_request
  449. rts
  450. ********************************
  451. **** INSERT COLS *****
  452. ********************************
  453. insert_cols
  454. ***send place to put palette in a0
  455. movem.l d0-d5/a0-a1,-(sp)
  456. move.l num_of_cols,d5
  457. divu #3,d5
  458. sub.w #1,d5
  459. move.l colour_map_ptr,a1
  460. insert_colours
  461. moveq #0,d3
  462. moveq #0,d4
  463. move.b (a1)+,d3
  464. lsl.w #4,d3
  465. or.b (a1)+,d3
  466. move.b (a1)+,d4
  467. lsr.w #4,d4
  468. or.w d4,d3
  469. move.w d3,(a0)+
  470. moveq #0,d0
  471. dbra d5,insert_colours
  472. movem.l (sp)+,d0-d5/a0-a1
  473. rts
  474. save_file_handle
  475. dc.l 0
  476. save_buffer
  477. dc.l 0
  478. rsreset
  479. RWIDTH rs.W 1
  480. RHEIGHT rs.W 1
  481. XPOS rs.W 1
  482. YPOS rs.W 1
  483. NUMPLANES rs.B 1
  484. PMASK rs.B 1
  485. TYPECOM rs.B 1
  486. PAD rs.B 1
  487. TRANSCOLOUR rs.W 1
  488. XASPECT rs.B 1
  489. YASPECT rs.B 1
  490. PAGEWIDTH rs.W 1
  491. PAGEHEIGHT rs.W 1
  492. ALLOCCOLS rs.B 1
  493. EVEN
  494. size_of_BMHD EQU 8+4+2+2+4+2
  495. save_bmhd
  496. ds.b size_of_BMHD
  497. EVEN
  498. PICTURE_DETAILS
  499. dc.l 0
  500. buffer
  501. ds.l 1
  502. pic_pointer
  503. dc.l 0
  504. header_size
  505. dc.l 0
  506. colour_map_ptr
  507. dc.l 0
  508. num_of_cols
  509. dc.l 0
  510. picture_data
  511. dc.l 0
  512. size_of_pic
  513. dc.l 0
  514. graphics_handle
  515. dc.l 0
  516. current_plane dc.w 0
  517. pixel_count dc.w 0
  518. plane_positions
  519. ds.l 6 ;maximum ever
  520. picture_struct1
  521. dc.w 0 ;pixel size x
  522. dc.w 0 ;pixel size y
  523. dc.w 0,0 ;x and y offsets
  524. dc.l 0 ;mem pointer
  525. dc.w 0 ;number of planes
  526. dc.l pic_1_palette
  527. picture_struct2
  528. dc.w 0 ;pixel size x
  529. dc.w 0 ;pixel size y
  530. dc.w 0,0 ;x and y offsets
  531. dc.l 0 ;mem pointer
  532. dc.w 0 ;number of planes
  533. dc.l pic_2_palette
  534. picture_struct3
  535. dc.w 0 ;pixel size x
  536. dc.w 0 ;pixel size y
  537. dc.w 0,0 ;x and y offsets
  538. dc.l 0 ;mem pointer
  539. dc.w 0 ;number of planes
  540. dc.l pic_3_palette
  541. picture_struct4
  542. dc.w 0 ;pixel size x
  543. dc.w 0 ;pixel size y
  544. dc.w 0,0 ;x and y offsets
  545. dc.l 0 ;mem pointer
  546. dc.w 0 ;number of planes
  547. dc.l pic_4_palette
  548. picture_struct5
  549. dc.w 0 ;pixel size x
  550. dc.w 0 ;pixel size y
  551. dc.w 0,0 ;x and y offsets
  552. dc.l 0 ;mem pointer
  553. dc.w 0 ;number of planes
  554. dc.l pic_5_palette
  555. pic_1_palette
  556. DC.W $222,$888,$555,$bbb,$666,$f33,$fff,$121
  557. DC.W $22d,$22d,$22d,$22d,$000,$c00,$0c0,$00c
  558. dc.w $f00,$f00,$f00,$f00
  559. dc.w $c00,$b00,$b00,$b00
  560. dc.w $900,$700,$700,$700
  561. dc.w $700,$700,$700,$700
  562. pic_2_palette
  563. DC.W $222,$888,$555,$bbb,$666,$f33,$fff,$121
  564. DC.W $22d,$22d,$22d,$22d,$000,$c00,$0c0,$00c
  565. dc.w $f00,$f00,$f00,$f00
  566. dc.w $c00,$b00,$b00,$b00
  567. dc.w $900,$700,$700,$700
  568. dc.w $700,$700,$700,$700
  569. pic_3_palette
  570. DC.W $222,$888,$555,$bbb,$666,$f33,$fff,$121
  571. DC.W $22d,$22d,$22d,$22d,$000,$c00,$0c0,$00c
  572. dc.w $f00,$f00,$f00,$f00
  573. dc.w $c00,$b00,$b00,$b00
  574. dc.w $900,$700,$700,$700
  575. dc.w $700,$700,$700,$700
  576. pic_4_palette
  577. DC.W $222,$888,$555,$bbb,$666,$f33,$fff,$121
  578. DC.W $22d,$22d,$22d,$22d,$000,$c00,$0c0,$00c
  579. dc.w $f00,$f00,$f00,$f00
  580. dc.w $c00,$b00,$b00,$b00
  581. dc.w $900,$700,$700,$700
  582. dc.w $700,$700,$700,$700
  583. pic_5_palette
  584. DC.W $222,$888,$555,$bbb,$666,$f33,$fff,$121
  585. DC.W $22d,$22d,$22d,$22d,$000,$c00,$0c0,$00c
  586. dc.w $f00,$f00,$f00,$f00
  587. dc.w $c00,$b00,$b00,$b00
  588. dc.w $900,$700,$700,$700
  589. dc.w $700,$700,$700,$700
  590. Default_Colours
  591. dc.b $f0,$f0,$f0
  592. dc.b $e0,$e0,$e0
  593. dc.b $d0,$d0,$d0
  594. dc.b $c0,$c0,$c0
  595. dc.b $b0,$b0,$b0
  596. dc.b $a0,$a0,$a0
  597. dc.b $90,$90,$90
  598. dc.b $80,$80,$80
  599. dc.b $70,$70,$70
  600. dc.b $60,$60,$60
  601. dc.b $50,$50,$50
  602. dc.b $40,$40,$40
  603. dc.b $30,$30,$30
  604. dc.b $20,$20,$20
  605. dc.b $10,$10,$10
  606. dc.b $00,$00,$00
  607. EVEN
  608. picture_pages
  609. dc.l picture_struct1
  610. dc.l picture_struct2
  611. dc.l picture_struct3
  612. dc.l picture_struct4
  613. dc.l picture_struct5
  614. page_pointer
  615. dc.l picture_struct1