cirrus_vga.c 90 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024
  1. /*
  2. * QEMU Cirrus CLGD 54xx VGA Emulator.
  3. *
  4. * Copyright (c) 2004 Fabrice Bellard
  5. * Copyright (c) 2004 Makoto Suzuki (suzu)
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  20. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. */
  25. /*
  26. * Reference: Finn Thogersons' VGADOC4b
  27. * available at http://home.worldonline.dk/~finth/
  28. */
  29. #include "hw/hw.h"
  30. #include "hw/pci/pci.h"
  31. #include "ui/console.h"
  32. #include "vga_int.h"
  33. #include "hw/loader.h"
  34. /*
  35. * TODO:
  36. * - destination write mask support not complete (bits 5..7)
  37. * - optimize linear mappings
  38. * - optimize bitblt functions
  39. */
  40. //#define DEBUG_CIRRUS
  41. //#define DEBUG_BITBLT
  42. /***************************************
  43. *
  44. * definitions
  45. *
  46. ***************************************/
  47. // ID
  48. #define CIRRUS_ID_CLGD5422 (0x23<<2)
  49. #define CIRRUS_ID_CLGD5426 (0x24<<2)
  50. #define CIRRUS_ID_CLGD5424 (0x25<<2)
  51. #define CIRRUS_ID_CLGD5428 (0x26<<2)
  52. #define CIRRUS_ID_CLGD5430 (0x28<<2)
  53. #define CIRRUS_ID_CLGD5434 (0x2A<<2)
  54. #define CIRRUS_ID_CLGD5436 (0x2B<<2)
  55. #define CIRRUS_ID_CLGD5446 (0x2E<<2)
  56. // sequencer 0x07
  57. #define CIRRUS_SR7_BPP_VGA 0x00
  58. #define CIRRUS_SR7_BPP_SVGA 0x01
  59. #define CIRRUS_SR7_BPP_MASK 0x0e
  60. #define CIRRUS_SR7_BPP_8 0x00
  61. #define CIRRUS_SR7_BPP_16_DOUBLEVCLK 0x02
  62. #define CIRRUS_SR7_BPP_24 0x04
  63. #define CIRRUS_SR7_BPP_16 0x06
  64. #define CIRRUS_SR7_BPP_32 0x08
  65. #define CIRRUS_SR7_ISAADDR_MASK 0xe0
  66. // sequencer 0x0f
  67. #define CIRRUS_MEMSIZE_512k 0x08
  68. #define CIRRUS_MEMSIZE_1M 0x10
  69. #define CIRRUS_MEMSIZE_2M 0x18
  70. #define CIRRUS_MEMFLAGS_BANKSWITCH 0x80 // bank switching is enabled.
  71. // sequencer 0x12
  72. #define CIRRUS_CURSOR_SHOW 0x01
  73. #define CIRRUS_CURSOR_HIDDENPEL 0x02
  74. #define CIRRUS_CURSOR_LARGE 0x04 // 64x64 if set, 32x32 if clear
  75. // sequencer 0x17
  76. #define CIRRUS_BUSTYPE_VLBFAST 0x10
  77. #define CIRRUS_BUSTYPE_PCI 0x20
  78. #define CIRRUS_BUSTYPE_VLBSLOW 0x30
  79. #define CIRRUS_BUSTYPE_ISA 0x38
  80. #define CIRRUS_MMIO_ENABLE 0x04
  81. #define CIRRUS_MMIO_USE_PCIADDR 0x40 // 0xb8000 if cleared.
  82. #define CIRRUS_MEMSIZEEXT_DOUBLE 0x80
  83. // control 0x0b
  84. #define CIRRUS_BANKING_DUAL 0x01
  85. #define CIRRUS_BANKING_GRANULARITY_16K 0x20 // set:16k, clear:4k
  86. // control 0x30
  87. #define CIRRUS_BLTMODE_BACKWARDS 0x01
  88. #define CIRRUS_BLTMODE_MEMSYSDEST 0x02
  89. #define CIRRUS_BLTMODE_MEMSYSSRC 0x04
  90. #define CIRRUS_BLTMODE_TRANSPARENTCOMP 0x08
  91. #define CIRRUS_BLTMODE_PATTERNCOPY 0x40
  92. #define CIRRUS_BLTMODE_COLOREXPAND 0x80
  93. #define CIRRUS_BLTMODE_PIXELWIDTHMASK 0x30
  94. #define CIRRUS_BLTMODE_PIXELWIDTH8 0x00
  95. #define CIRRUS_BLTMODE_PIXELWIDTH16 0x10
  96. #define CIRRUS_BLTMODE_PIXELWIDTH24 0x20
  97. #define CIRRUS_BLTMODE_PIXELWIDTH32 0x30
  98. // control 0x31
  99. #define CIRRUS_BLT_BUSY 0x01
  100. #define CIRRUS_BLT_START 0x02
  101. #define CIRRUS_BLT_RESET 0x04
  102. #define CIRRUS_BLT_FIFOUSED 0x10
  103. #define CIRRUS_BLT_AUTOSTART 0x80
  104. // control 0x32
  105. #define CIRRUS_ROP_0 0x00
  106. #define CIRRUS_ROP_SRC_AND_DST 0x05
  107. #define CIRRUS_ROP_NOP 0x06
  108. #define CIRRUS_ROP_SRC_AND_NOTDST 0x09
  109. #define CIRRUS_ROP_NOTDST 0x0b
  110. #define CIRRUS_ROP_SRC 0x0d
  111. #define CIRRUS_ROP_1 0x0e
  112. #define CIRRUS_ROP_NOTSRC_AND_DST 0x50
  113. #define CIRRUS_ROP_SRC_XOR_DST 0x59
  114. #define CIRRUS_ROP_SRC_OR_DST 0x6d
  115. #define CIRRUS_ROP_NOTSRC_OR_NOTDST 0x90
  116. #define CIRRUS_ROP_SRC_NOTXOR_DST 0x95
  117. #define CIRRUS_ROP_SRC_OR_NOTDST 0xad
  118. #define CIRRUS_ROP_NOTSRC 0xd0
  119. #define CIRRUS_ROP_NOTSRC_OR_DST 0xd6
  120. #define CIRRUS_ROP_NOTSRC_AND_NOTDST 0xda
  121. #define CIRRUS_ROP_NOP_INDEX 2
  122. #define CIRRUS_ROP_SRC_INDEX 5
  123. // control 0x33
  124. #define CIRRUS_BLTMODEEXT_SOLIDFILL 0x04
  125. #define CIRRUS_BLTMODEEXT_COLOREXPINV 0x02
  126. #define CIRRUS_BLTMODEEXT_DWORDGRANULARITY 0x01
  127. // memory-mapped IO
  128. #define CIRRUS_MMIO_BLTBGCOLOR 0x00 // dword
  129. #define CIRRUS_MMIO_BLTFGCOLOR 0x04 // dword
  130. #define CIRRUS_MMIO_BLTWIDTH 0x08 // word
  131. #define CIRRUS_MMIO_BLTHEIGHT 0x0a // word
  132. #define CIRRUS_MMIO_BLTDESTPITCH 0x0c // word
  133. #define CIRRUS_MMIO_BLTSRCPITCH 0x0e // word
  134. #define CIRRUS_MMIO_BLTDESTADDR 0x10 // dword
  135. #define CIRRUS_MMIO_BLTSRCADDR 0x14 // dword
  136. #define CIRRUS_MMIO_BLTWRITEMASK 0x17 // byte
  137. #define CIRRUS_MMIO_BLTMODE 0x18 // byte
  138. #define CIRRUS_MMIO_BLTROP 0x1a // byte
  139. #define CIRRUS_MMIO_BLTMODEEXT 0x1b // byte
  140. #define CIRRUS_MMIO_BLTTRANSPARENTCOLOR 0x1c // word?
  141. #define CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK 0x20 // word?
  142. #define CIRRUS_MMIO_LINEARDRAW_START_X 0x24 // word
  143. #define CIRRUS_MMIO_LINEARDRAW_START_Y 0x26 // word
  144. #define CIRRUS_MMIO_LINEARDRAW_END_X 0x28 // word
  145. #define CIRRUS_MMIO_LINEARDRAW_END_Y 0x2a // word
  146. #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_INC 0x2c // byte
  147. #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ROLLOVER 0x2d // byte
  148. #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_MASK 0x2e // byte
  149. #define CIRRUS_MMIO_LINEARDRAW_LINESTYLE_ACCUM 0x2f // byte
  150. #define CIRRUS_MMIO_BRESENHAM_K1 0x30 // word
  151. #define CIRRUS_MMIO_BRESENHAM_K3 0x32 // word
  152. #define CIRRUS_MMIO_BRESENHAM_ERROR 0x34 // word
  153. #define CIRRUS_MMIO_BRESENHAM_DELTA_MAJOR 0x36 // word
  154. #define CIRRUS_MMIO_BRESENHAM_DIRECTION 0x38 // byte
  155. #define CIRRUS_MMIO_LINEDRAW_MODE 0x39 // byte
  156. #define CIRRUS_MMIO_BLTSTATUS 0x40 // byte
  157. #define CIRRUS_PNPMMIO_SIZE 0x1000
  158. #define BLTUNSAFE(s) \
  159. ( \
  160. ( /* check dst is within bounds */ \
  161. (s)->cirrus_blt_height * ABS((s)->cirrus_blt_dstpitch) \
  162. + ((s)->cirrus_blt_dstaddr & (s)->cirrus_addr_mask) > \
  163. (s)->vga.vram_size \
  164. ) || \
  165. ( /* check src is within bounds */ \
  166. (s)->cirrus_blt_height * ABS((s)->cirrus_blt_srcpitch) \
  167. + ((s)->cirrus_blt_srcaddr & (s)->cirrus_addr_mask) > \
  168. (s)->vga.vram_size \
  169. ) \
  170. )
  171. struct CirrusVGAState;
  172. typedef void (*cirrus_bitblt_rop_t) (struct CirrusVGAState *s,
  173. uint8_t * dst, const uint8_t * src,
  174. int dstpitch, int srcpitch,
  175. int bltwidth, int bltheight);
  176. typedef void (*cirrus_fill_t)(struct CirrusVGAState *s,
  177. uint8_t *dst, int dst_pitch, int width, int height);
  178. typedef struct CirrusVGAState {
  179. VGACommonState vga;
  180. MemoryRegion cirrus_vga_io;
  181. MemoryRegion cirrus_linear_io;
  182. MemoryRegion cirrus_linear_bitblt_io;
  183. MemoryRegion cirrus_mmio_io;
  184. MemoryRegion pci_bar;
  185. bool linear_vram; /* vga.vram mapped over cirrus_linear_io */
  186. MemoryRegion low_mem_container; /* container for 0xa0000-0xc0000 */
  187. MemoryRegion low_mem; /* always mapped, overridden by: */
  188. MemoryRegion cirrus_bank[2]; /* aliases at 0xa0000-0xb0000 */
  189. uint32_t cirrus_addr_mask;
  190. uint32_t linear_mmio_mask;
  191. uint8_t cirrus_shadow_gr0;
  192. uint8_t cirrus_shadow_gr1;
  193. uint8_t cirrus_hidden_dac_lockindex;
  194. uint8_t cirrus_hidden_dac_data;
  195. uint32_t cirrus_bank_base[2];
  196. uint32_t cirrus_bank_limit[2];
  197. uint8_t cirrus_hidden_palette[48];
  198. uint32_t hw_cursor_x;
  199. uint32_t hw_cursor_y;
  200. int cirrus_blt_pixelwidth;
  201. int cirrus_blt_width;
  202. int cirrus_blt_height;
  203. int cirrus_blt_dstpitch;
  204. int cirrus_blt_srcpitch;
  205. uint32_t cirrus_blt_fgcol;
  206. uint32_t cirrus_blt_bgcol;
  207. uint32_t cirrus_blt_dstaddr;
  208. uint32_t cirrus_blt_srcaddr;
  209. uint8_t cirrus_blt_mode;
  210. uint8_t cirrus_blt_modeext;
  211. cirrus_bitblt_rop_t cirrus_rop;
  212. #define CIRRUS_BLTBUFSIZE (2048 * 4) /* one line width */
  213. uint8_t cirrus_bltbuf[CIRRUS_BLTBUFSIZE];
  214. uint8_t *cirrus_srcptr;
  215. uint8_t *cirrus_srcptr_end;
  216. uint32_t cirrus_srccounter;
  217. /* hwcursor display state */
  218. int last_hw_cursor_size;
  219. int last_hw_cursor_x;
  220. int last_hw_cursor_y;
  221. int last_hw_cursor_y_start;
  222. int last_hw_cursor_y_end;
  223. int real_vram_size; /* XXX: suppress that */
  224. int device_id;
  225. int bustype;
  226. } CirrusVGAState;
  227. typedef struct PCICirrusVGAState {
  228. PCIDevice dev;
  229. CirrusVGAState cirrus_vga;
  230. } PCICirrusVGAState;
  231. #define TYPE_ISA_CIRRUS_VGA "isa-cirrus-vga"
  232. #define ISA_CIRRUS_VGA(obj) \
  233. OBJECT_CHECK(ISACirrusVGAState, (obj), TYPE_ISA_CIRRUS_VGA)
  234. typedef struct ISACirrusVGAState {
  235. ISADevice parent_obj;
  236. CirrusVGAState cirrus_vga;
  237. } ISACirrusVGAState;
  238. static uint8_t rop_to_index[256];
  239. /***************************************
  240. *
  241. * prototypes.
  242. *
  243. ***************************************/
  244. static void cirrus_bitblt_reset(CirrusVGAState *s);
  245. static void cirrus_update_memory_access(CirrusVGAState *s);
  246. /***************************************
  247. *
  248. * raster operations
  249. *
  250. ***************************************/
  251. static void cirrus_bitblt_rop_nop(CirrusVGAState *s,
  252. uint8_t *dst,const uint8_t *src,
  253. int dstpitch,int srcpitch,
  254. int bltwidth,int bltheight)
  255. {
  256. }
  257. static void cirrus_bitblt_fill_nop(CirrusVGAState *s,
  258. uint8_t *dst,
  259. int dstpitch, int bltwidth,int bltheight)
  260. {
  261. }
  262. #define ROP_NAME 0
  263. #define ROP_FN(d, s) 0
  264. #include "cirrus_vga_rop.h"
  265. #define ROP_NAME src_and_dst
  266. #define ROP_FN(d, s) (s) & (d)
  267. #include "cirrus_vga_rop.h"
  268. #define ROP_NAME src_and_notdst
  269. #define ROP_FN(d, s) (s) & (~(d))
  270. #include "cirrus_vga_rop.h"
  271. #define ROP_NAME notdst
  272. #define ROP_FN(d, s) ~(d)
  273. #include "cirrus_vga_rop.h"
  274. #define ROP_NAME src
  275. #define ROP_FN(d, s) s
  276. #include "cirrus_vga_rop.h"
  277. #define ROP_NAME 1
  278. #define ROP_FN(d, s) ~0
  279. #include "cirrus_vga_rop.h"
  280. #define ROP_NAME notsrc_and_dst
  281. #define ROP_FN(d, s) (~(s)) & (d)
  282. #include "cirrus_vga_rop.h"
  283. #define ROP_NAME src_xor_dst
  284. #define ROP_FN(d, s) (s) ^ (d)
  285. #include "cirrus_vga_rop.h"
  286. #define ROP_NAME src_or_dst
  287. #define ROP_FN(d, s) (s) | (d)
  288. #include "cirrus_vga_rop.h"
  289. #define ROP_NAME notsrc_or_notdst
  290. #define ROP_FN(d, s) (~(s)) | (~(d))
  291. #include "cirrus_vga_rop.h"
  292. #define ROP_NAME src_notxor_dst
  293. #define ROP_FN(d, s) ~((s) ^ (d))
  294. #include "cirrus_vga_rop.h"
  295. #define ROP_NAME src_or_notdst
  296. #define ROP_FN(d, s) (s) | (~(d))
  297. #include "cirrus_vga_rop.h"
  298. #define ROP_NAME notsrc
  299. #define ROP_FN(d, s) (~(s))
  300. #include "cirrus_vga_rop.h"
  301. #define ROP_NAME notsrc_or_dst
  302. #define ROP_FN(d, s) (~(s)) | (d)
  303. #include "cirrus_vga_rop.h"
  304. #define ROP_NAME notsrc_and_notdst
  305. #define ROP_FN(d, s) (~(s)) & (~(d))
  306. #include "cirrus_vga_rop.h"
  307. static const cirrus_bitblt_rop_t cirrus_fwd_rop[16] = {
  308. cirrus_bitblt_rop_fwd_0,
  309. cirrus_bitblt_rop_fwd_src_and_dst,
  310. cirrus_bitblt_rop_nop,
  311. cirrus_bitblt_rop_fwd_src_and_notdst,
  312. cirrus_bitblt_rop_fwd_notdst,
  313. cirrus_bitblt_rop_fwd_src,
  314. cirrus_bitblt_rop_fwd_1,
  315. cirrus_bitblt_rop_fwd_notsrc_and_dst,
  316. cirrus_bitblt_rop_fwd_src_xor_dst,
  317. cirrus_bitblt_rop_fwd_src_or_dst,
  318. cirrus_bitblt_rop_fwd_notsrc_or_notdst,
  319. cirrus_bitblt_rop_fwd_src_notxor_dst,
  320. cirrus_bitblt_rop_fwd_src_or_notdst,
  321. cirrus_bitblt_rop_fwd_notsrc,
  322. cirrus_bitblt_rop_fwd_notsrc_or_dst,
  323. cirrus_bitblt_rop_fwd_notsrc_and_notdst,
  324. };
  325. static const cirrus_bitblt_rop_t cirrus_bkwd_rop[16] = {
  326. cirrus_bitblt_rop_bkwd_0,
  327. cirrus_bitblt_rop_bkwd_src_and_dst,
  328. cirrus_bitblt_rop_nop,
  329. cirrus_bitblt_rop_bkwd_src_and_notdst,
  330. cirrus_bitblt_rop_bkwd_notdst,
  331. cirrus_bitblt_rop_bkwd_src,
  332. cirrus_bitblt_rop_bkwd_1,
  333. cirrus_bitblt_rop_bkwd_notsrc_and_dst,
  334. cirrus_bitblt_rop_bkwd_src_xor_dst,
  335. cirrus_bitblt_rop_bkwd_src_or_dst,
  336. cirrus_bitblt_rop_bkwd_notsrc_or_notdst,
  337. cirrus_bitblt_rop_bkwd_src_notxor_dst,
  338. cirrus_bitblt_rop_bkwd_src_or_notdst,
  339. cirrus_bitblt_rop_bkwd_notsrc,
  340. cirrus_bitblt_rop_bkwd_notsrc_or_dst,
  341. cirrus_bitblt_rop_bkwd_notsrc_and_notdst,
  342. };
  343. #define TRANSP_ROP(name) {\
  344. name ## _8,\
  345. name ## _16,\
  346. }
  347. #define TRANSP_NOP(func) {\
  348. func,\
  349. func,\
  350. }
  351. static const cirrus_bitblt_rop_t cirrus_fwd_transp_rop[16][2] = {
  352. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_0),
  353. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_dst),
  354. TRANSP_NOP(cirrus_bitblt_rop_nop),
  355. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_and_notdst),
  356. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notdst),
  357. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src),
  358. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_1),
  359. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_dst),
  360. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_xor_dst),
  361. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_dst),
  362. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_notdst),
  363. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_notxor_dst),
  364. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_src_or_notdst),
  365. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc),
  366. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_or_dst),
  367. TRANSP_ROP(cirrus_bitblt_rop_fwd_transp_notsrc_and_notdst),
  368. };
  369. static const cirrus_bitblt_rop_t cirrus_bkwd_transp_rop[16][2] = {
  370. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_0),
  371. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_dst),
  372. TRANSP_NOP(cirrus_bitblt_rop_nop),
  373. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_and_notdst),
  374. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notdst),
  375. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src),
  376. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_1),
  377. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_dst),
  378. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_xor_dst),
  379. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_dst),
  380. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_notdst),
  381. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_notxor_dst),
  382. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_src_or_notdst),
  383. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc),
  384. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_or_dst),
  385. TRANSP_ROP(cirrus_bitblt_rop_bkwd_transp_notsrc_and_notdst),
  386. };
  387. #define ROP2(name) {\
  388. name ## _8,\
  389. name ## _16,\
  390. name ## _24,\
  391. name ## _32,\
  392. }
  393. #define ROP_NOP2(func) {\
  394. func,\
  395. func,\
  396. func,\
  397. func,\
  398. }
  399. static const cirrus_bitblt_rop_t cirrus_patternfill[16][4] = {
  400. ROP2(cirrus_patternfill_0),
  401. ROP2(cirrus_patternfill_src_and_dst),
  402. ROP_NOP2(cirrus_bitblt_rop_nop),
  403. ROP2(cirrus_patternfill_src_and_notdst),
  404. ROP2(cirrus_patternfill_notdst),
  405. ROP2(cirrus_patternfill_src),
  406. ROP2(cirrus_patternfill_1),
  407. ROP2(cirrus_patternfill_notsrc_and_dst),
  408. ROP2(cirrus_patternfill_src_xor_dst),
  409. ROP2(cirrus_patternfill_src_or_dst),
  410. ROP2(cirrus_patternfill_notsrc_or_notdst),
  411. ROP2(cirrus_patternfill_src_notxor_dst),
  412. ROP2(cirrus_patternfill_src_or_notdst),
  413. ROP2(cirrus_patternfill_notsrc),
  414. ROP2(cirrus_patternfill_notsrc_or_dst),
  415. ROP2(cirrus_patternfill_notsrc_and_notdst),
  416. };
  417. static const cirrus_bitblt_rop_t cirrus_colorexpand_transp[16][4] = {
  418. ROP2(cirrus_colorexpand_transp_0),
  419. ROP2(cirrus_colorexpand_transp_src_and_dst),
  420. ROP_NOP2(cirrus_bitblt_rop_nop),
  421. ROP2(cirrus_colorexpand_transp_src_and_notdst),
  422. ROP2(cirrus_colorexpand_transp_notdst),
  423. ROP2(cirrus_colorexpand_transp_src),
  424. ROP2(cirrus_colorexpand_transp_1),
  425. ROP2(cirrus_colorexpand_transp_notsrc_and_dst),
  426. ROP2(cirrus_colorexpand_transp_src_xor_dst),
  427. ROP2(cirrus_colorexpand_transp_src_or_dst),
  428. ROP2(cirrus_colorexpand_transp_notsrc_or_notdst),
  429. ROP2(cirrus_colorexpand_transp_src_notxor_dst),
  430. ROP2(cirrus_colorexpand_transp_src_or_notdst),
  431. ROP2(cirrus_colorexpand_transp_notsrc),
  432. ROP2(cirrus_colorexpand_transp_notsrc_or_dst),
  433. ROP2(cirrus_colorexpand_transp_notsrc_and_notdst),
  434. };
  435. static const cirrus_bitblt_rop_t cirrus_colorexpand[16][4] = {
  436. ROP2(cirrus_colorexpand_0),
  437. ROP2(cirrus_colorexpand_src_and_dst),
  438. ROP_NOP2(cirrus_bitblt_rop_nop),
  439. ROP2(cirrus_colorexpand_src_and_notdst),
  440. ROP2(cirrus_colorexpand_notdst),
  441. ROP2(cirrus_colorexpand_src),
  442. ROP2(cirrus_colorexpand_1),
  443. ROP2(cirrus_colorexpand_notsrc_and_dst),
  444. ROP2(cirrus_colorexpand_src_xor_dst),
  445. ROP2(cirrus_colorexpand_src_or_dst),
  446. ROP2(cirrus_colorexpand_notsrc_or_notdst),
  447. ROP2(cirrus_colorexpand_src_notxor_dst),
  448. ROP2(cirrus_colorexpand_src_or_notdst),
  449. ROP2(cirrus_colorexpand_notsrc),
  450. ROP2(cirrus_colorexpand_notsrc_or_dst),
  451. ROP2(cirrus_colorexpand_notsrc_and_notdst),
  452. };
  453. static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern_transp[16][4] = {
  454. ROP2(cirrus_colorexpand_pattern_transp_0),
  455. ROP2(cirrus_colorexpand_pattern_transp_src_and_dst),
  456. ROP_NOP2(cirrus_bitblt_rop_nop),
  457. ROP2(cirrus_colorexpand_pattern_transp_src_and_notdst),
  458. ROP2(cirrus_colorexpand_pattern_transp_notdst),
  459. ROP2(cirrus_colorexpand_pattern_transp_src),
  460. ROP2(cirrus_colorexpand_pattern_transp_1),
  461. ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_dst),
  462. ROP2(cirrus_colorexpand_pattern_transp_src_xor_dst),
  463. ROP2(cirrus_colorexpand_pattern_transp_src_or_dst),
  464. ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_notdst),
  465. ROP2(cirrus_colorexpand_pattern_transp_src_notxor_dst),
  466. ROP2(cirrus_colorexpand_pattern_transp_src_or_notdst),
  467. ROP2(cirrus_colorexpand_pattern_transp_notsrc),
  468. ROP2(cirrus_colorexpand_pattern_transp_notsrc_or_dst),
  469. ROP2(cirrus_colorexpand_pattern_transp_notsrc_and_notdst),
  470. };
  471. static const cirrus_bitblt_rop_t cirrus_colorexpand_pattern[16][4] = {
  472. ROP2(cirrus_colorexpand_pattern_0),
  473. ROP2(cirrus_colorexpand_pattern_src_and_dst),
  474. ROP_NOP2(cirrus_bitblt_rop_nop),
  475. ROP2(cirrus_colorexpand_pattern_src_and_notdst),
  476. ROP2(cirrus_colorexpand_pattern_notdst),
  477. ROP2(cirrus_colorexpand_pattern_src),
  478. ROP2(cirrus_colorexpand_pattern_1),
  479. ROP2(cirrus_colorexpand_pattern_notsrc_and_dst),
  480. ROP2(cirrus_colorexpand_pattern_src_xor_dst),
  481. ROP2(cirrus_colorexpand_pattern_src_or_dst),
  482. ROP2(cirrus_colorexpand_pattern_notsrc_or_notdst),
  483. ROP2(cirrus_colorexpand_pattern_src_notxor_dst),
  484. ROP2(cirrus_colorexpand_pattern_src_or_notdst),
  485. ROP2(cirrus_colorexpand_pattern_notsrc),
  486. ROP2(cirrus_colorexpand_pattern_notsrc_or_dst),
  487. ROP2(cirrus_colorexpand_pattern_notsrc_and_notdst),
  488. };
  489. static const cirrus_fill_t cirrus_fill[16][4] = {
  490. ROP2(cirrus_fill_0),
  491. ROP2(cirrus_fill_src_and_dst),
  492. ROP_NOP2(cirrus_bitblt_fill_nop),
  493. ROP2(cirrus_fill_src_and_notdst),
  494. ROP2(cirrus_fill_notdst),
  495. ROP2(cirrus_fill_src),
  496. ROP2(cirrus_fill_1),
  497. ROP2(cirrus_fill_notsrc_and_dst),
  498. ROP2(cirrus_fill_src_xor_dst),
  499. ROP2(cirrus_fill_src_or_dst),
  500. ROP2(cirrus_fill_notsrc_or_notdst),
  501. ROP2(cirrus_fill_src_notxor_dst),
  502. ROP2(cirrus_fill_src_or_notdst),
  503. ROP2(cirrus_fill_notsrc),
  504. ROP2(cirrus_fill_notsrc_or_dst),
  505. ROP2(cirrus_fill_notsrc_and_notdst),
  506. };
  507. static inline void cirrus_bitblt_fgcol(CirrusVGAState *s)
  508. {
  509. unsigned int color;
  510. switch (s->cirrus_blt_pixelwidth) {
  511. case 1:
  512. s->cirrus_blt_fgcol = s->cirrus_shadow_gr1;
  513. break;
  514. case 2:
  515. color = s->cirrus_shadow_gr1 | (s->vga.gr[0x11] << 8);
  516. s->cirrus_blt_fgcol = le16_to_cpu(color);
  517. break;
  518. case 3:
  519. s->cirrus_blt_fgcol = s->cirrus_shadow_gr1 |
  520. (s->vga.gr[0x11] << 8) | (s->vga.gr[0x13] << 16);
  521. break;
  522. default:
  523. case 4:
  524. color = s->cirrus_shadow_gr1 | (s->vga.gr[0x11] << 8) |
  525. (s->vga.gr[0x13] << 16) | (s->vga.gr[0x15] << 24);
  526. s->cirrus_blt_fgcol = le32_to_cpu(color);
  527. break;
  528. }
  529. }
  530. static inline void cirrus_bitblt_bgcol(CirrusVGAState *s)
  531. {
  532. unsigned int color;
  533. switch (s->cirrus_blt_pixelwidth) {
  534. case 1:
  535. s->cirrus_blt_bgcol = s->cirrus_shadow_gr0;
  536. break;
  537. case 2:
  538. color = s->cirrus_shadow_gr0 | (s->vga.gr[0x10] << 8);
  539. s->cirrus_blt_bgcol = le16_to_cpu(color);
  540. break;
  541. case 3:
  542. s->cirrus_blt_bgcol = s->cirrus_shadow_gr0 |
  543. (s->vga.gr[0x10] << 8) | (s->vga.gr[0x12] << 16);
  544. break;
  545. default:
  546. case 4:
  547. color = s->cirrus_shadow_gr0 | (s->vga.gr[0x10] << 8) |
  548. (s->vga.gr[0x12] << 16) | (s->vga.gr[0x14] << 24);
  549. s->cirrus_blt_bgcol = le32_to_cpu(color);
  550. break;
  551. }
  552. }
  553. static void cirrus_invalidate_region(CirrusVGAState * s, int off_begin,
  554. int off_pitch, int bytesperline,
  555. int lines)
  556. {
  557. int y;
  558. int off_cur;
  559. int off_cur_end;
  560. for (y = 0; y < lines; y++) {
  561. off_cur = off_begin;
  562. off_cur_end = (off_cur + bytesperline) & s->cirrus_addr_mask;
  563. memory_region_set_dirty(&s->vga.vram, off_cur, off_cur_end - off_cur);
  564. off_begin += off_pitch;
  565. }
  566. }
  567. static int cirrus_bitblt_common_patterncopy(CirrusVGAState * s,
  568. const uint8_t * src)
  569. {
  570. uint8_t *dst;
  571. dst = s->vga.vram_ptr + (s->cirrus_blt_dstaddr & s->cirrus_addr_mask);
  572. if (BLTUNSAFE(s))
  573. return 0;
  574. (*s->cirrus_rop) (s, dst, src,
  575. s->cirrus_blt_dstpitch, 0,
  576. s->cirrus_blt_width, s->cirrus_blt_height);
  577. cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
  578. s->cirrus_blt_dstpitch, s->cirrus_blt_width,
  579. s->cirrus_blt_height);
  580. return 1;
  581. }
  582. /* fill */
  583. static int cirrus_bitblt_solidfill(CirrusVGAState *s, int blt_rop)
  584. {
  585. cirrus_fill_t rop_func;
  586. if (BLTUNSAFE(s))
  587. return 0;
  588. rop_func = cirrus_fill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  589. rop_func(s, s->vga.vram_ptr + (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
  590. s->cirrus_blt_dstpitch,
  591. s->cirrus_blt_width, s->cirrus_blt_height);
  592. cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
  593. s->cirrus_blt_dstpitch, s->cirrus_blt_width,
  594. s->cirrus_blt_height);
  595. cirrus_bitblt_reset(s);
  596. return 1;
  597. }
  598. /***************************************
  599. *
  600. * bitblt (video-to-video)
  601. *
  602. ***************************************/
  603. static int cirrus_bitblt_videotovideo_patterncopy(CirrusVGAState * s)
  604. {
  605. return cirrus_bitblt_common_patterncopy(s,
  606. s->vga.vram_ptr + ((s->cirrus_blt_srcaddr & ~7) &
  607. s->cirrus_addr_mask));
  608. }
  609. static void cirrus_do_copy(CirrusVGAState *s, int dst, int src, int w, int h)
  610. {
  611. int sx = 0, sy = 0;
  612. int dx = 0, dy = 0;
  613. int depth = 0;
  614. int notify = 0;
  615. /* make sure to only copy if it's a plain copy ROP */
  616. if (*s->cirrus_rop == cirrus_bitblt_rop_fwd_src ||
  617. *s->cirrus_rop == cirrus_bitblt_rop_bkwd_src) {
  618. int width, height;
  619. depth = s->vga.get_bpp(&s->vga) / 8;
  620. s->vga.get_resolution(&s->vga, &width, &height);
  621. /* extra x, y */
  622. sx = (src % ABS(s->cirrus_blt_srcpitch)) / depth;
  623. sy = (src / ABS(s->cirrus_blt_srcpitch));
  624. dx = (dst % ABS(s->cirrus_blt_dstpitch)) / depth;
  625. dy = (dst / ABS(s->cirrus_blt_dstpitch));
  626. /* normalize width */
  627. w /= depth;
  628. /* if we're doing a backward copy, we have to adjust
  629. our x/y to be the upper left corner (instead of the lower
  630. right corner) */
  631. if (s->cirrus_blt_dstpitch < 0) {
  632. sx -= (s->cirrus_blt_width / depth) - 1;
  633. dx -= (s->cirrus_blt_width / depth) - 1;
  634. sy -= s->cirrus_blt_height - 1;
  635. dy -= s->cirrus_blt_height - 1;
  636. }
  637. /* are we in the visible portion of memory? */
  638. if (sx >= 0 && sy >= 0 && dx >= 0 && dy >= 0 &&
  639. (sx + w) <= width && (sy + h) <= height &&
  640. (dx + w) <= width && (dy + h) <= height) {
  641. notify = 1;
  642. }
  643. }
  644. /* we have to flush all pending changes so that the copy
  645. is generated at the appropriate moment in time */
  646. if (notify)
  647. graphic_hw_update(s->vga.con);
  648. (*s->cirrus_rop) (s, s->vga.vram_ptr +
  649. (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
  650. s->vga.vram_ptr +
  651. (s->cirrus_blt_srcaddr & s->cirrus_addr_mask),
  652. s->cirrus_blt_dstpitch, s->cirrus_blt_srcpitch,
  653. s->cirrus_blt_width, s->cirrus_blt_height);
  654. if (notify) {
  655. qemu_console_copy(s->vga.con,
  656. sx, sy, dx, dy,
  657. s->cirrus_blt_width / depth,
  658. s->cirrus_blt_height);
  659. }
  660. /* we don't have to notify the display that this portion has
  661. changed since qemu_console_copy implies this */
  662. cirrus_invalidate_region(s, s->cirrus_blt_dstaddr,
  663. s->cirrus_blt_dstpitch, s->cirrus_blt_width,
  664. s->cirrus_blt_height);
  665. }
  666. static int cirrus_bitblt_videotovideo_copy(CirrusVGAState * s)
  667. {
  668. if (BLTUNSAFE(s))
  669. return 0;
  670. cirrus_do_copy(s, s->cirrus_blt_dstaddr - s->vga.start_addr,
  671. s->cirrus_blt_srcaddr - s->vga.start_addr,
  672. s->cirrus_blt_width, s->cirrus_blt_height);
  673. return 1;
  674. }
  675. /***************************************
  676. *
  677. * bitblt (cpu-to-video)
  678. *
  679. ***************************************/
  680. static void cirrus_bitblt_cputovideo_next(CirrusVGAState * s)
  681. {
  682. int copy_count;
  683. uint8_t *end_ptr;
  684. if (s->cirrus_srccounter > 0) {
  685. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
  686. cirrus_bitblt_common_patterncopy(s, s->cirrus_bltbuf);
  687. the_end:
  688. s->cirrus_srccounter = 0;
  689. cirrus_bitblt_reset(s);
  690. } else {
  691. /* at least one scan line */
  692. do {
  693. (*s->cirrus_rop)(s, s->vga.vram_ptr +
  694. (s->cirrus_blt_dstaddr & s->cirrus_addr_mask),
  695. s->cirrus_bltbuf, 0, 0, s->cirrus_blt_width, 1);
  696. cirrus_invalidate_region(s, s->cirrus_blt_dstaddr, 0,
  697. s->cirrus_blt_width, 1);
  698. s->cirrus_blt_dstaddr += s->cirrus_blt_dstpitch;
  699. s->cirrus_srccounter -= s->cirrus_blt_srcpitch;
  700. if (s->cirrus_srccounter <= 0)
  701. goto the_end;
  702. /* more bytes than needed can be transferred because of
  703. word alignment, so we keep them for the next line */
  704. /* XXX: keep alignment to speed up transfer */
  705. end_ptr = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
  706. copy_count = s->cirrus_srcptr_end - end_ptr;
  707. memmove(s->cirrus_bltbuf, end_ptr, copy_count);
  708. s->cirrus_srcptr = s->cirrus_bltbuf + copy_count;
  709. s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
  710. } while (s->cirrus_srcptr >= s->cirrus_srcptr_end);
  711. }
  712. }
  713. }
  714. /***************************************
  715. *
  716. * bitblt wrapper
  717. *
  718. ***************************************/
  719. static void cirrus_bitblt_reset(CirrusVGAState * s)
  720. {
  721. int need_update;
  722. s->vga.gr[0x31] &=
  723. ~(CIRRUS_BLT_START | CIRRUS_BLT_BUSY | CIRRUS_BLT_FIFOUSED);
  724. need_update = s->cirrus_srcptr != &s->cirrus_bltbuf[0]
  725. || s->cirrus_srcptr_end != &s->cirrus_bltbuf[0];
  726. s->cirrus_srcptr = &s->cirrus_bltbuf[0];
  727. s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
  728. s->cirrus_srccounter = 0;
  729. if (!need_update)
  730. return;
  731. cirrus_update_memory_access(s);
  732. }
  733. static int cirrus_bitblt_cputovideo(CirrusVGAState * s)
  734. {
  735. int w;
  736. s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_MEMSYSSRC;
  737. s->cirrus_srcptr = &s->cirrus_bltbuf[0];
  738. s->cirrus_srcptr_end = &s->cirrus_bltbuf[0];
  739. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
  740. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
  741. s->cirrus_blt_srcpitch = 8;
  742. } else {
  743. /* XXX: check for 24 bpp */
  744. s->cirrus_blt_srcpitch = 8 * 8 * s->cirrus_blt_pixelwidth;
  745. }
  746. s->cirrus_srccounter = s->cirrus_blt_srcpitch;
  747. } else {
  748. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
  749. w = s->cirrus_blt_width / s->cirrus_blt_pixelwidth;
  750. if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_DWORDGRANULARITY)
  751. s->cirrus_blt_srcpitch = ((w + 31) >> 5);
  752. else
  753. s->cirrus_blt_srcpitch = ((w + 7) >> 3);
  754. } else {
  755. /* always align input size to 32 bits */
  756. s->cirrus_blt_srcpitch = (s->cirrus_blt_width + 3) & ~3;
  757. }
  758. s->cirrus_srccounter = s->cirrus_blt_srcpitch * s->cirrus_blt_height;
  759. }
  760. s->cirrus_srcptr = s->cirrus_bltbuf;
  761. s->cirrus_srcptr_end = s->cirrus_bltbuf + s->cirrus_blt_srcpitch;
  762. cirrus_update_memory_access(s);
  763. return 1;
  764. }
  765. static int cirrus_bitblt_videotocpu(CirrusVGAState * s)
  766. {
  767. /* XXX */
  768. #ifdef DEBUG_BITBLT
  769. printf("cirrus: bitblt (video to cpu) is not implemented yet\n");
  770. #endif
  771. return 0;
  772. }
  773. static int cirrus_bitblt_videotovideo(CirrusVGAState * s)
  774. {
  775. int ret;
  776. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
  777. ret = cirrus_bitblt_videotovideo_patterncopy(s);
  778. } else {
  779. ret = cirrus_bitblt_videotovideo_copy(s);
  780. }
  781. if (ret)
  782. cirrus_bitblt_reset(s);
  783. return ret;
  784. }
  785. static void cirrus_bitblt_start(CirrusVGAState * s)
  786. {
  787. uint8_t blt_rop;
  788. s->vga.gr[0x31] |= CIRRUS_BLT_BUSY;
  789. s->cirrus_blt_width = (s->vga.gr[0x20] | (s->vga.gr[0x21] << 8)) + 1;
  790. s->cirrus_blt_height = (s->vga.gr[0x22] | (s->vga.gr[0x23] << 8)) + 1;
  791. s->cirrus_blt_dstpitch = (s->vga.gr[0x24] | (s->vga.gr[0x25] << 8));
  792. s->cirrus_blt_srcpitch = (s->vga.gr[0x26] | (s->vga.gr[0x27] << 8));
  793. s->cirrus_blt_dstaddr =
  794. (s->vga.gr[0x28] | (s->vga.gr[0x29] << 8) | (s->vga.gr[0x2a] << 16));
  795. s->cirrus_blt_srcaddr =
  796. (s->vga.gr[0x2c] | (s->vga.gr[0x2d] << 8) | (s->vga.gr[0x2e] << 16));
  797. s->cirrus_blt_mode = s->vga.gr[0x30];
  798. s->cirrus_blt_modeext = s->vga.gr[0x33];
  799. blt_rop = s->vga.gr[0x32];
  800. #ifdef DEBUG_BITBLT
  801. printf("rop=0x%02x mode=0x%02x modeext=0x%02x w=%d h=%d dpitch=%d spitch=%d daddr=0x%08x saddr=0x%08x writemask=0x%02x\n",
  802. blt_rop,
  803. s->cirrus_blt_mode,
  804. s->cirrus_blt_modeext,
  805. s->cirrus_blt_width,
  806. s->cirrus_blt_height,
  807. s->cirrus_blt_dstpitch,
  808. s->cirrus_blt_srcpitch,
  809. s->cirrus_blt_dstaddr,
  810. s->cirrus_blt_srcaddr,
  811. s->vga.gr[0x2f]);
  812. #endif
  813. switch (s->cirrus_blt_mode & CIRRUS_BLTMODE_PIXELWIDTHMASK) {
  814. case CIRRUS_BLTMODE_PIXELWIDTH8:
  815. s->cirrus_blt_pixelwidth = 1;
  816. break;
  817. case CIRRUS_BLTMODE_PIXELWIDTH16:
  818. s->cirrus_blt_pixelwidth = 2;
  819. break;
  820. case CIRRUS_BLTMODE_PIXELWIDTH24:
  821. s->cirrus_blt_pixelwidth = 3;
  822. break;
  823. case CIRRUS_BLTMODE_PIXELWIDTH32:
  824. s->cirrus_blt_pixelwidth = 4;
  825. break;
  826. default:
  827. #ifdef DEBUG_BITBLT
  828. printf("cirrus: bitblt - pixel width is unknown\n");
  829. #endif
  830. goto bitblt_ignore;
  831. }
  832. s->cirrus_blt_mode &= ~CIRRUS_BLTMODE_PIXELWIDTHMASK;
  833. if ((s->
  834. cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSSRC |
  835. CIRRUS_BLTMODE_MEMSYSDEST))
  836. == (CIRRUS_BLTMODE_MEMSYSSRC | CIRRUS_BLTMODE_MEMSYSDEST)) {
  837. #ifdef DEBUG_BITBLT
  838. printf("cirrus: bitblt - memory-to-memory copy is requested\n");
  839. #endif
  840. goto bitblt_ignore;
  841. }
  842. if ((s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_SOLIDFILL) &&
  843. (s->cirrus_blt_mode & (CIRRUS_BLTMODE_MEMSYSDEST |
  844. CIRRUS_BLTMODE_TRANSPARENTCOMP |
  845. CIRRUS_BLTMODE_PATTERNCOPY |
  846. CIRRUS_BLTMODE_COLOREXPAND)) ==
  847. (CIRRUS_BLTMODE_PATTERNCOPY | CIRRUS_BLTMODE_COLOREXPAND)) {
  848. cirrus_bitblt_fgcol(s);
  849. cirrus_bitblt_solidfill(s, blt_rop);
  850. } else {
  851. if ((s->cirrus_blt_mode & (CIRRUS_BLTMODE_COLOREXPAND |
  852. CIRRUS_BLTMODE_PATTERNCOPY)) ==
  853. CIRRUS_BLTMODE_COLOREXPAND) {
  854. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
  855. if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
  856. cirrus_bitblt_bgcol(s);
  857. else
  858. cirrus_bitblt_fgcol(s);
  859. s->cirrus_rop = cirrus_colorexpand_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  860. } else {
  861. cirrus_bitblt_fgcol(s);
  862. cirrus_bitblt_bgcol(s);
  863. s->cirrus_rop = cirrus_colorexpand[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  864. }
  865. } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_PATTERNCOPY) {
  866. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_COLOREXPAND) {
  867. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
  868. if (s->cirrus_blt_modeext & CIRRUS_BLTMODEEXT_COLOREXPINV)
  869. cirrus_bitblt_bgcol(s);
  870. else
  871. cirrus_bitblt_fgcol(s);
  872. s->cirrus_rop = cirrus_colorexpand_pattern_transp[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  873. } else {
  874. cirrus_bitblt_fgcol(s);
  875. cirrus_bitblt_bgcol(s);
  876. s->cirrus_rop = cirrus_colorexpand_pattern[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  877. }
  878. } else {
  879. s->cirrus_rop = cirrus_patternfill[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  880. }
  881. } else {
  882. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_TRANSPARENTCOMP) {
  883. if (s->cirrus_blt_pixelwidth > 2) {
  884. printf("src transparent without colorexpand must be 8bpp or 16bpp\n");
  885. goto bitblt_ignore;
  886. }
  887. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
  888. s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
  889. s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
  890. s->cirrus_rop = cirrus_bkwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  891. } else {
  892. s->cirrus_rop = cirrus_fwd_transp_rop[rop_to_index[blt_rop]][s->cirrus_blt_pixelwidth - 1];
  893. }
  894. } else {
  895. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_BACKWARDS) {
  896. s->cirrus_blt_dstpitch = -s->cirrus_blt_dstpitch;
  897. s->cirrus_blt_srcpitch = -s->cirrus_blt_srcpitch;
  898. s->cirrus_rop = cirrus_bkwd_rop[rop_to_index[blt_rop]];
  899. } else {
  900. s->cirrus_rop = cirrus_fwd_rop[rop_to_index[blt_rop]];
  901. }
  902. }
  903. }
  904. // setup bitblt engine.
  905. if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSSRC) {
  906. if (!cirrus_bitblt_cputovideo(s))
  907. goto bitblt_ignore;
  908. } else if (s->cirrus_blt_mode & CIRRUS_BLTMODE_MEMSYSDEST) {
  909. if (!cirrus_bitblt_videotocpu(s))
  910. goto bitblt_ignore;
  911. } else {
  912. if (!cirrus_bitblt_videotovideo(s))
  913. goto bitblt_ignore;
  914. }
  915. }
  916. return;
  917. bitblt_ignore:;
  918. cirrus_bitblt_reset(s);
  919. }
  920. static void cirrus_write_bitblt(CirrusVGAState * s, unsigned reg_value)
  921. {
  922. unsigned old_value;
  923. old_value = s->vga.gr[0x31];
  924. s->vga.gr[0x31] = reg_value;
  925. if (((old_value & CIRRUS_BLT_RESET) != 0) &&
  926. ((reg_value & CIRRUS_BLT_RESET) == 0)) {
  927. cirrus_bitblt_reset(s);
  928. } else if (((old_value & CIRRUS_BLT_START) == 0) &&
  929. ((reg_value & CIRRUS_BLT_START) != 0)) {
  930. cirrus_bitblt_start(s);
  931. }
  932. }
  933. /***************************************
  934. *
  935. * basic parameters
  936. *
  937. ***************************************/
  938. static void cirrus_get_offsets(VGACommonState *s1,
  939. uint32_t *pline_offset,
  940. uint32_t *pstart_addr,
  941. uint32_t *pline_compare)
  942. {
  943. CirrusVGAState * s = container_of(s1, CirrusVGAState, vga);
  944. uint32_t start_addr, line_offset, line_compare;
  945. line_offset = s->vga.cr[0x13]
  946. | ((s->vga.cr[0x1b] & 0x10) << 4);
  947. line_offset <<= 3;
  948. *pline_offset = line_offset;
  949. start_addr = (s->vga.cr[0x0c] << 8)
  950. | s->vga.cr[0x0d]
  951. | ((s->vga.cr[0x1b] & 0x01) << 16)
  952. | ((s->vga.cr[0x1b] & 0x0c) << 15)
  953. | ((s->vga.cr[0x1d] & 0x80) << 12);
  954. *pstart_addr = start_addr;
  955. line_compare = s->vga.cr[0x18] |
  956. ((s->vga.cr[0x07] & 0x10) << 4) |
  957. ((s->vga.cr[0x09] & 0x40) << 3);
  958. *pline_compare = line_compare;
  959. }
  960. static uint32_t cirrus_get_bpp16_depth(CirrusVGAState * s)
  961. {
  962. uint32_t ret = 16;
  963. switch (s->cirrus_hidden_dac_data & 0xf) {
  964. case 0:
  965. ret = 15;
  966. break; /* Sierra HiColor */
  967. case 1:
  968. ret = 16;
  969. break; /* XGA HiColor */
  970. default:
  971. #ifdef DEBUG_CIRRUS
  972. printf("cirrus: invalid DAC value %x in 16bpp\n",
  973. (s->cirrus_hidden_dac_data & 0xf));
  974. #endif
  975. ret = 15; /* XXX */
  976. break;
  977. }
  978. return ret;
  979. }
  980. static int cirrus_get_bpp(VGACommonState *s1)
  981. {
  982. CirrusVGAState * s = container_of(s1, CirrusVGAState, vga);
  983. uint32_t ret = 8;
  984. if ((s->vga.sr[0x07] & 0x01) != 0) {
  985. /* Cirrus SVGA */
  986. switch (s->vga.sr[0x07] & CIRRUS_SR7_BPP_MASK) {
  987. case CIRRUS_SR7_BPP_8:
  988. ret = 8;
  989. break;
  990. case CIRRUS_SR7_BPP_16_DOUBLEVCLK:
  991. ret = cirrus_get_bpp16_depth(s);
  992. break;
  993. case CIRRUS_SR7_BPP_24:
  994. ret = 24;
  995. break;
  996. case CIRRUS_SR7_BPP_16:
  997. ret = cirrus_get_bpp16_depth(s);
  998. break;
  999. case CIRRUS_SR7_BPP_32:
  1000. ret = 32;
  1001. break;
  1002. default:
  1003. #ifdef DEBUG_CIRRUS
  1004. printf("cirrus: unknown bpp - sr7=%x\n", s->vga.sr[0x7]);
  1005. #endif
  1006. ret = 8;
  1007. break;
  1008. }
  1009. } else {
  1010. /* VGA */
  1011. ret = 0;
  1012. }
  1013. return ret;
  1014. }
  1015. static void cirrus_get_resolution(VGACommonState *s, int *pwidth, int *pheight)
  1016. {
  1017. int width, height;
  1018. width = (s->cr[0x01] + 1) * 8;
  1019. height = s->cr[0x12] |
  1020. ((s->cr[0x07] & 0x02) << 7) |
  1021. ((s->cr[0x07] & 0x40) << 3);
  1022. height = (height + 1);
  1023. /* interlace support */
  1024. if (s->cr[0x1a] & 0x01)
  1025. height = height * 2;
  1026. *pwidth = width;
  1027. *pheight = height;
  1028. }
  1029. /***************************************
  1030. *
  1031. * bank memory
  1032. *
  1033. ***************************************/
  1034. static void cirrus_update_bank_ptr(CirrusVGAState * s, unsigned bank_index)
  1035. {
  1036. unsigned offset;
  1037. unsigned limit;
  1038. if ((s->vga.gr[0x0b] & 0x01) != 0) /* dual bank */
  1039. offset = s->vga.gr[0x09 + bank_index];
  1040. else /* single bank */
  1041. offset = s->vga.gr[0x09];
  1042. if ((s->vga.gr[0x0b] & 0x20) != 0)
  1043. offset <<= 14;
  1044. else
  1045. offset <<= 12;
  1046. if (s->real_vram_size <= offset)
  1047. limit = 0;
  1048. else
  1049. limit = s->real_vram_size - offset;
  1050. if (((s->vga.gr[0x0b] & 0x01) == 0) && (bank_index != 0)) {
  1051. if (limit > 0x8000) {
  1052. offset += 0x8000;
  1053. limit -= 0x8000;
  1054. } else {
  1055. limit = 0;
  1056. }
  1057. }
  1058. if (limit > 0) {
  1059. s->cirrus_bank_base[bank_index] = offset;
  1060. s->cirrus_bank_limit[bank_index] = limit;
  1061. } else {
  1062. s->cirrus_bank_base[bank_index] = 0;
  1063. s->cirrus_bank_limit[bank_index] = 0;
  1064. }
  1065. }
  1066. /***************************************
  1067. *
  1068. * I/O access between 0x3c4-0x3c5
  1069. *
  1070. ***************************************/
  1071. static int cirrus_vga_read_sr(CirrusVGAState * s)
  1072. {
  1073. switch (s->vga.sr_index) {
  1074. case 0x00: // Standard VGA
  1075. case 0x01: // Standard VGA
  1076. case 0x02: // Standard VGA
  1077. case 0x03: // Standard VGA
  1078. case 0x04: // Standard VGA
  1079. return s->vga.sr[s->vga.sr_index];
  1080. case 0x06: // Unlock Cirrus extensions
  1081. return s->vga.sr[s->vga.sr_index];
  1082. case 0x10:
  1083. case 0x30:
  1084. case 0x50:
  1085. case 0x70: // Graphics Cursor X
  1086. case 0x90:
  1087. case 0xb0:
  1088. case 0xd0:
  1089. case 0xf0: // Graphics Cursor X
  1090. return s->vga.sr[0x10];
  1091. case 0x11:
  1092. case 0x31:
  1093. case 0x51:
  1094. case 0x71: // Graphics Cursor Y
  1095. case 0x91:
  1096. case 0xb1:
  1097. case 0xd1:
  1098. case 0xf1: // Graphics Cursor Y
  1099. return s->vga.sr[0x11];
  1100. case 0x05: // ???
  1101. case 0x07: // Extended Sequencer Mode
  1102. case 0x08: // EEPROM Control
  1103. case 0x09: // Scratch Register 0
  1104. case 0x0a: // Scratch Register 1
  1105. case 0x0b: // VCLK 0
  1106. case 0x0c: // VCLK 1
  1107. case 0x0d: // VCLK 2
  1108. case 0x0e: // VCLK 3
  1109. case 0x0f: // DRAM Control
  1110. case 0x12: // Graphics Cursor Attribute
  1111. case 0x13: // Graphics Cursor Pattern Address
  1112. case 0x14: // Scratch Register 2
  1113. case 0x15: // Scratch Register 3
  1114. case 0x16: // Performance Tuning Register
  1115. case 0x17: // Configuration Readback and Extended Control
  1116. case 0x18: // Signature Generator Control
  1117. case 0x19: // Signal Generator Result
  1118. case 0x1a: // Signal Generator Result
  1119. case 0x1b: // VCLK 0 Denominator & Post
  1120. case 0x1c: // VCLK 1 Denominator & Post
  1121. case 0x1d: // VCLK 2 Denominator & Post
  1122. case 0x1e: // VCLK 3 Denominator & Post
  1123. case 0x1f: // BIOS Write Enable and MCLK select
  1124. #ifdef DEBUG_CIRRUS
  1125. printf("cirrus: handled inport sr_index %02x\n", s->vga.sr_index);
  1126. #endif
  1127. return s->vga.sr[s->vga.sr_index];
  1128. default:
  1129. #ifdef DEBUG_CIRRUS
  1130. printf("cirrus: inport sr_index %02x\n", s->vga.sr_index);
  1131. #endif
  1132. return 0xff;
  1133. break;
  1134. }
  1135. }
  1136. static void cirrus_vga_write_sr(CirrusVGAState * s, uint32_t val)
  1137. {
  1138. switch (s->vga.sr_index) {
  1139. case 0x00: // Standard VGA
  1140. case 0x01: // Standard VGA
  1141. case 0x02: // Standard VGA
  1142. case 0x03: // Standard VGA
  1143. case 0x04: // Standard VGA
  1144. s->vga.sr[s->vga.sr_index] = val & sr_mask[s->vga.sr_index];
  1145. if (s->vga.sr_index == 1)
  1146. s->vga.update_retrace_info(&s->vga);
  1147. break;
  1148. case 0x06: // Unlock Cirrus extensions
  1149. val &= 0x17;
  1150. if (val == 0x12) {
  1151. s->vga.sr[s->vga.sr_index] = 0x12;
  1152. } else {
  1153. s->vga.sr[s->vga.sr_index] = 0x0f;
  1154. }
  1155. break;
  1156. case 0x10:
  1157. case 0x30:
  1158. case 0x50:
  1159. case 0x70: // Graphics Cursor X
  1160. case 0x90:
  1161. case 0xb0:
  1162. case 0xd0:
  1163. case 0xf0: // Graphics Cursor X
  1164. s->vga.sr[0x10] = val;
  1165. s->hw_cursor_x = (val << 3) | (s->vga.sr_index >> 5);
  1166. break;
  1167. case 0x11:
  1168. case 0x31:
  1169. case 0x51:
  1170. case 0x71: // Graphics Cursor Y
  1171. case 0x91:
  1172. case 0xb1:
  1173. case 0xd1:
  1174. case 0xf1: // Graphics Cursor Y
  1175. s->vga.sr[0x11] = val;
  1176. s->hw_cursor_y = (val << 3) | (s->vga.sr_index >> 5);
  1177. break;
  1178. case 0x07: // Extended Sequencer Mode
  1179. cirrus_update_memory_access(s);
  1180. case 0x08: // EEPROM Control
  1181. case 0x09: // Scratch Register 0
  1182. case 0x0a: // Scratch Register 1
  1183. case 0x0b: // VCLK 0
  1184. case 0x0c: // VCLK 1
  1185. case 0x0d: // VCLK 2
  1186. case 0x0e: // VCLK 3
  1187. case 0x0f: // DRAM Control
  1188. case 0x12: // Graphics Cursor Attribute
  1189. case 0x13: // Graphics Cursor Pattern Address
  1190. case 0x14: // Scratch Register 2
  1191. case 0x15: // Scratch Register 3
  1192. case 0x16: // Performance Tuning Register
  1193. case 0x18: // Signature Generator Control
  1194. case 0x19: // Signature Generator Result
  1195. case 0x1a: // Signature Generator Result
  1196. case 0x1b: // VCLK 0 Denominator & Post
  1197. case 0x1c: // VCLK 1 Denominator & Post
  1198. case 0x1d: // VCLK 2 Denominator & Post
  1199. case 0x1e: // VCLK 3 Denominator & Post
  1200. case 0x1f: // BIOS Write Enable and MCLK select
  1201. s->vga.sr[s->vga.sr_index] = val;
  1202. #ifdef DEBUG_CIRRUS
  1203. printf("cirrus: handled outport sr_index %02x, sr_value %02x\n",
  1204. s->vga.sr_index, val);
  1205. #endif
  1206. break;
  1207. case 0x17: // Configuration Readback and Extended Control
  1208. s->vga.sr[s->vga.sr_index] = (s->vga.sr[s->vga.sr_index] & 0x38)
  1209. | (val & 0xc7);
  1210. cirrus_update_memory_access(s);
  1211. break;
  1212. default:
  1213. #ifdef DEBUG_CIRRUS
  1214. printf("cirrus: outport sr_index %02x, sr_value %02x\n",
  1215. s->vga.sr_index, val);
  1216. #endif
  1217. break;
  1218. }
  1219. }
  1220. /***************************************
  1221. *
  1222. * I/O access at 0x3c6
  1223. *
  1224. ***************************************/
  1225. static int cirrus_read_hidden_dac(CirrusVGAState * s)
  1226. {
  1227. if (++s->cirrus_hidden_dac_lockindex == 5) {
  1228. s->cirrus_hidden_dac_lockindex = 0;
  1229. return s->cirrus_hidden_dac_data;
  1230. }
  1231. return 0xff;
  1232. }
  1233. static void cirrus_write_hidden_dac(CirrusVGAState * s, int reg_value)
  1234. {
  1235. if (s->cirrus_hidden_dac_lockindex == 4) {
  1236. s->cirrus_hidden_dac_data = reg_value;
  1237. #if defined(DEBUG_CIRRUS)
  1238. printf("cirrus: outport hidden DAC, value %02x\n", reg_value);
  1239. #endif
  1240. }
  1241. s->cirrus_hidden_dac_lockindex = 0;
  1242. }
  1243. /***************************************
  1244. *
  1245. * I/O access at 0x3c9
  1246. *
  1247. ***************************************/
  1248. static int cirrus_vga_read_palette(CirrusVGAState * s)
  1249. {
  1250. int val;
  1251. if ((s->vga.sr[0x12] & CIRRUS_CURSOR_HIDDENPEL)) {
  1252. val = s->cirrus_hidden_palette[(s->vga.dac_read_index & 0x0f) * 3 +
  1253. s->vga.dac_sub_index];
  1254. } else {
  1255. val = s->vga.palette[s->vga.dac_read_index * 3 + s->vga.dac_sub_index];
  1256. }
  1257. if (++s->vga.dac_sub_index == 3) {
  1258. s->vga.dac_sub_index = 0;
  1259. s->vga.dac_read_index++;
  1260. }
  1261. return val;
  1262. }
  1263. static void cirrus_vga_write_palette(CirrusVGAState * s, int reg_value)
  1264. {
  1265. s->vga.dac_cache[s->vga.dac_sub_index] = reg_value;
  1266. if (++s->vga.dac_sub_index == 3) {
  1267. if ((s->vga.sr[0x12] & CIRRUS_CURSOR_HIDDENPEL)) {
  1268. memcpy(&s->cirrus_hidden_palette[(s->vga.dac_write_index & 0x0f) * 3],
  1269. s->vga.dac_cache, 3);
  1270. } else {
  1271. memcpy(&s->vga.palette[s->vga.dac_write_index * 3], s->vga.dac_cache, 3);
  1272. }
  1273. /* XXX update cursor */
  1274. s->vga.dac_sub_index = 0;
  1275. s->vga.dac_write_index++;
  1276. }
  1277. }
  1278. /***************************************
  1279. *
  1280. * I/O access between 0x3ce-0x3cf
  1281. *
  1282. ***************************************/
  1283. static int cirrus_vga_read_gr(CirrusVGAState * s, unsigned reg_index)
  1284. {
  1285. switch (reg_index) {
  1286. case 0x00: // Standard VGA, BGCOLOR 0x000000ff
  1287. return s->cirrus_shadow_gr0;
  1288. case 0x01: // Standard VGA, FGCOLOR 0x000000ff
  1289. return s->cirrus_shadow_gr1;
  1290. case 0x02: // Standard VGA
  1291. case 0x03: // Standard VGA
  1292. case 0x04: // Standard VGA
  1293. case 0x06: // Standard VGA
  1294. case 0x07: // Standard VGA
  1295. case 0x08: // Standard VGA
  1296. return s->vga.gr[s->vga.gr_index];
  1297. case 0x05: // Standard VGA, Cirrus extended mode
  1298. default:
  1299. break;
  1300. }
  1301. if (reg_index < 0x3a) {
  1302. return s->vga.gr[reg_index];
  1303. } else {
  1304. #ifdef DEBUG_CIRRUS
  1305. printf("cirrus: inport gr_index %02x\n", reg_index);
  1306. #endif
  1307. return 0xff;
  1308. }
  1309. }
  1310. static void
  1311. cirrus_vga_write_gr(CirrusVGAState * s, unsigned reg_index, int reg_value)
  1312. {
  1313. #if defined(DEBUG_BITBLT) && 0
  1314. printf("gr%02x: %02x\n", reg_index, reg_value);
  1315. #endif
  1316. switch (reg_index) {
  1317. case 0x00: // Standard VGA, BGCOLOR 0x000000ff
  1318. s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
  1319. s->cirrus_shadow_gr0 = reg_value;
  1320. break;
  1321. case 0x01: // Standard VGA, FGCOLOR 0x000000ff
  1322. s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
  1323. s->cirrus_shadow_gr1 = reg_value;
  1324. break;
  1325. case 0x02: // Standard VGA
  1326. case 0x03: // Standard VGA
  1327. case 0x04: // Standard VGA
  1328. case 0x06: // Standard VGA
  1329. case 0x07: // Standard VGA
  1330. case 0x08: // Standard VGA
  1331. s->vga.gr[reg_index] = reg_value & gr_mask[reg_index];
  1332. break;
  1333. case 0x05: // Standard VGA, Cirrus extended mode
  1334. s->vga.gr[reg_index] = reg_value & 0x7f;
  1335. cirrus_update_memory_access(s);
  1336. break;
  1337. case 0x09: // bank offset #0
  1338. case 0x0A: // bank offset #1
  1339. s->vga.gr[reg_index] = reg_value;
  1340. cirrus_update_bank_ptr(s, 0);
  1341. cirrus_update_bank_ptr(s, 1);
  1342. cirrus_update_memory_access(s);
  1343. break;
  1344. case 0x0B:
  1345. s->vga.gr[reg_index] = reg_value;
  1346. cirrus_update_bank_ptr(s, 0);
  1347. cirrus_update_bank_ptr(s, 1);
  1348. cirrus_update_memory_access(s);
  1349. break;
  1350. case 0x10: // BGCOLOR 0x0000ff00
  1351. case 0x11: // FGCOLOR 0x0000ff00
  1352. case 0x12: // BGCOLOR 0x00ff0000
  1353. case 0x13: // FGCOLOR 0x00ff0000
  1354. case 0x14: // BGCOLOR 0xff000000
  1355. case 0x15: // FGCOLOR 0xff000000
  1356. case 0x20: // BLT WIDTH 0x0000ff
  1357. case 0x22: // BLT HEIGHT 0x0000ff
  1358. case 0x24: // BLT DEST PITCH 0x0000ff
  1359. case 0x26: // BLT SRC PITCH 0x0000ff
  1360. case 0x28: // BLT DEST ADDR 0x0000ff
  1361. case 0x29: // BLT DEST ADDR 0x00ff00
  1362. case 0x2c: // BLT SRC ADDR 0x0000ff
  1363. case 0x2d: // BLT SRC ADDR 0x00ff00
  1364. case 0x2f: // BLT WRITEMASK
  1365. case 0x30: // BLT MODE
  1366. case 0x32: // RASTER OP
  1367. case 0x33: // BLT MODEEXT
  1368. case 0x34: // BLT TRANSPARENT COLOR 0x00ff
  1369. case 0x35: // BLT TRANSPARENT COLOR 0xff00
  1370. case 0x38: // BLT TRANSPARENT COLOR MASK 0x00ff
  1371. case 0x39: // BLT TRANSPARENT COLOR MASK 0xff00
  1372. s->vga.gr[reg_index] = reg_value;
  1373. break;
  1374. case 0x21: // BLT WIDTH 0x001f00
  1375. case 0x23: // BLT HEIGHT 0x001f00
  1376. case 0x25: // BLT DEST PITCH 0x001f00
  1377. case 0x27: // BLT SRC PITCH 0x001f00
  1378. s->vga.gr[reg_index] = reg_value & 0x1f;
  1379. break;
  1380. case 0x2a: // BLT DEST ADDR 0x3f0000
  1381. s->vga.gr[reg_index] = reg_value & 0x3f;
  1382. /* if auto start mode, starts bit blt now */
  1383. if (s->vga.gr[0x31] & CIRRUS_BLT_AUTOSTART) {
  1384. cirrus_bitblt_start(s);
  1385. }
  1386. break;
  1387. case 0x2e: // BLT SRC ADDR 0x3f0000
  1388. s->vga.gr[reg_index] = reg_value & 0x3f;
  1389. break;
  1390. case 0x31: // BLT STATUS/START
  1391. cirrus_write_bitblt(s, reg_value);
  1392. break;
  1393. default:
  1394. #ifdef DEBUG_CIRRUS
  1395. printf("cirrus: outport gr_index %02x, gr_value %02x\n", reg_index,
  1396. reg_value);
  1397. #endif
  1398. break;
  1399. }
  1400. }
  1401. /***************************************
  1402. *
  1403. * I/O access between 0x3d4-0x3d5
  1404. *
  1405. ***************************************/
  1406. static int cirrus_vga_read_cr(CirrusVGAState * s, unsigned reg_index)
  1407. {
  1408. switch (reg_index) {
  1409. case 0x00: // Standard VGA
  1410. case 0x01: // Standard VGA
  1411. case 0x02: // Standard VGA
  1412. case 0x03: // Standard VGA
  1413. case 0x04: // Standard VGA
  1414. case 0x05: // Standard VGA
  1415. case 0x06: // Standard VGA
  1416. case 0x07: // Standard VGA
  1417. case 0x08: // Standard VGA
  1418. case 0x09: // Standard VGA
  1419. case 0x0a: // Standard VGA
  1420. case 0x0b: // Standard VGA
  1421. case 0x0c: // Standard VGA
  1422. case 0x0d: // Standard VGA
  1423. case 0x0e: // Standard VGA
  1424. case 0x0f: // Standard VGA
  1425. case 0x10: // Standard VGA
  1426. case 0x11: // Standard VGA
  1427. case 0x12: // Standard VGA
  1428. case 0x13: // Standard VGA
  1429. case 0x14: // Standard VGA
  1430. case 0x15: // Standard VGA
  1431. case 0x16: // Standard VGA
  1432. case 0x17: // Standard VGA
  1433. case 0x18: // Standard VGA
  1434. return s->vga.cr[s->vga.cr_index];
  1435. case 0x24: // Attribute Controller Toggle Readback (R)
  1436. return (s->vga.ar_flip_flop << 7);
  1437. case 0x19: // Interlace End
  1438. case 0x1a: // Miscellaneous Control
  1439. case 0x1b: // Extended Display Control
  1440. case 0x1c: // Sync Adjust and Genlock
  1441. case 0x1d: // Overlay Extended Control
  1442. case 0x22: // Graphics Data Latches Readback (R)
  1443. case 0x25: // Part Status
  1444. case 0x27: // Part ID (R)
  1445. return s->vga.cr[s->vga.cr_index];
  1446. case 0x26: // Attribute Controller Index Readback (R)
  1447. return s->vga.ar_index & 0x3f;
  1448. break;
  1449. default:
  1450. #ifdef DEBUG_CIRRUS
  1451. printf("cirrus: inport cr_index %02x\n", reg_index);
  1452. #endif
  1453. return 0xff;
  1454. }
  1455. }
  1456. static void cirrus_vga_write_cr(CirrusVGAState * s, int reg_value)
  1457. {
  1458. switch (s->vga.cr_index) {
  1459. case 0x00: // Standard VGA
  1460. case 0x01: // Standard VGA
  1461. case 0x02: // Standard VGA
  1462. case 0x03: // Standard VGA
  1463. case 0x04: // Standard VGA
  1464. case 0x05: // Standard VGA
  1465. case 0x06: // Standard VGA
  1466. case 0x07: // Standard VGA
  1467. case 0x08: // Standard VGA
  1468. case 0x09: // Standard VGA
  1469. case 0x0a: // Standard VGA
  1470. case 0x0b: // Standard VGA
  1471. case 0x0c: // Standard VGA
  1472. case 0x0d: // Standard VGA
  1473. case 0x0e: // Standard VGA
  1474. case 0x0f: // Standard VGA
  1475. case 0x10: // Standard VGA
  1476. case 0x11: // Standard VGA
  1477. case 0x12: // Standard VGA
  1478. case 0x13: // Standard VGA
  1479. case 0x14: // Standard VGA
  1480. case 0x15: // Standard VGA
  1481. case 0x16: // Standard VGA
  1482. case 0x17: // Standard VGA
  1483. case 0x18: // Standard VGA
  1484. /* handle CR0-7 protection */
  1485. if ((s->vga.cr[0x11] & 0x80) && s->vga.cr_index <= 7) {
  1486. /* can always write bit 4 of CR7 */
  1487. if (s->vga.cr_index == 7)
  1488. s->vga.cr[7] = (s->vga.cr[7] & ~0x10) | (reg_value & 0x10);
  1489. return;
  1490. }
  1491. s->vga.cr[s->vga.cr_index] = reg_value;
  1492. switch(s->vga.cr_index) {
  1493. case 0x00:
  1494. case 0x04:
  1495. case 0x05:
  1496. case 0x06:
  1497. case 0x07:
  1498. case 0x11:
  1499. case 0x17:
  1500. s->vga.update_retrace_info(&s->vga);
  1501. break;
  1502. }
  1503. break;
  1504. case 0x19: // Interlace End
  1505. case 0x1a: // Miscellaneous Control
  1506. case 0x1b: // Extended Display Control
  1507. case 0x1c: // Sync Adjust and Genlock
  1508. case 0x1d: // Overlay Extended Control
  1509. s->vga.cr[s->vga.cr_index] = reg_value;
  1510. #ifdef DEBUG_CIRRUS
  1511. printf("cirrus: handled outport cr_index %02x, cr_value %02x\n",
  1512. s->vga.cr_index, reg_value);
  1513. #endif
  1514. break;
  1515. case 0x22: // Graphics Data Latches Readback (R)
  1516. case 0x24: // Attribute Controller Toggle Readback (R)
  1517. case 0x26: // Attribute Controller Index Readback (R)
  1518. case 0x27: // Part ID (R)
  1519. break;
  1520. case 0x25: // Part Status
  1521. default:
  1522. #ifdef DEBUG_CIRRUS
  1523. printf("cirrus: outport cr_index %02x, cr_value %02x\n",
  1524. s->vga.cr_index, reg_value);
  1525. #endif
  1526. break;
  1527. }
  1528. }
  1529. /***************************************
  1530. *
  1531. * memory-mapped I/O (bitblt)
  1532. *
  1533. ***************************************/
  1534. static uint8_t cirrus_mmio_blt_read(CirrusVGAState * s, unsigned address)
  1535. {
  1536. int value = 0xff;
  1537. switch (address) {
  1538. case (CIRRUS_MMIO_BLTBGCOLOR + 0):
  1539. value = cirrus_vga_read_gr(s, 0x00);
  1540. break;
  1541. case (CIRRUS_MMIO_BLTBGCOLOR + 1):
  1542. value = cirrus_vga_read_gr(s, 0x10);
  1543. break;
  1544. case (CIRRUS_MMIO_BLTBGCOLOR + 2):
  1545. value = cirrus_vga_read_gr(s, 0x12);
  1546. break;
  1547. case (CIRRUS_MMIO_BLTBGCOLOR + 3):
  1548. value = cirrus_vga_read_gr(s, 0x14);
  1549. break;
  1550. case (CIRRUS_MMIO_BLTFGCOLOR + 0):
  1551. value = cirrus_vga_read_gr(s, 0x01);
  1552. break;
  1553. case (CIRRUS_MMIO_BLTFGCOLOR + 1):
  1554. value = cirrus_vga_read_gr(s, 0x11);
  1555. break;
  1556. case (CIRRUS_MMIO_BLTFGCOLOR + 2):
  1557. value = cirrus_vga_read_gr(s, 0x13);
  1558. break;
  1559. case (CIRRUS_MMIO_BLTFGCOLOR + 3):
  1560. value = cirrus_vga_read_gr(s, 0x15);
  1561. break;
  1562. case (CIRRUS_MMIO_BLTWIDTH + 0):
  1563. value = cirrus_vga_read_gr(s, 0x20);
  1564. break;
  1565. case (CIRRUS_MMIO_BLTWIDTH + 1):
  1566. value = cirrus_vga_read_gr(s, 0x21);
  1567. break;
  1568. case (CIRRUS_MMIO_BLTHEIGHT + 0):
  1569. value = cirrus_vga_read_gr(s, 0x22);
  1570. break;
  1571. case (CIRRUS_MMIO_BLTHEIGHT + 1):
  1572. value = cirrus_vga_read_gr(s, 0x23);
  1573. break;
  1574. case (CIRRUS_MMIO_BLTDESTPITCH + 0):
  1575. value = cirrus_vga_read_gr(s, 0x24);
  1576. break;
  1577. case (CIRRUS_MMIO_BLTDESTPITCH + 1):
  1578. value = cirrus_vga_read_gr(s, 0x25);
  1579. break;
  1580. case (CIRRUS_MMIO_BLTSRCPITCH + 0):
  1581. value = cirrus_vga_read_gr(s, 0x26);
  1582. break;
  1583. case (CIRRUS_MMIO_BLTSRCPITCH + 1):
  1584. value = cirrus_vga_read_gr(s, 0x27);
  1585. break;
  1586. case (CIRRUS_MMIO_BLTDESTADDR + 0):
  1587. value = cirrus_vga_read_gr(s, 0x28);
  1588. break;
  1589. case (CIRRUS_MMIO_BLTDESTADDR + 1):
  1590. value = cirrus_vga_read_gr(s, 0x29);
  1591. break;
  1592. case (CIRRUS_MMIO_BLTDESTADDR + 2):
  1593. value = cirrus_vga_read_gr(s, 0x2a);
  1594. break;
  1595. case (CIRRUS_MMIO_BLTSRCADDR + 0):
  1596. value = cirrus_vga_read_gr(s, 0x2c);
  1597. break;
  1598. case (CIRRUS_MMIO_BLTSRCADDR + 1):
  1599. value = cirrus_vga_read_gr(s, 0x2d);
  1600. break;
  1601. case (CIRRUS_MMIO_BLTSRCADDR + 2):
  1602. value = cirrus_vga_read_gr(s, 0x2e);
  1603. break;
  1604. case CIRRUS_MMIO_BLTWRITEMASK:
  1605. value = cirrus_vga_read_gr(s, 0x2f);
  1606. break;
  1607. case CIRRUS_MMIO_BLTMODE:
  1608. value = cirrus_vga_read_gr(s, 0x30);
  1609. break;
  1610. case CIRRUS_MMIO_BLTROP:
  1611. value = cirrus_vga_read_gr(s, 0x32);
  1612. break;
  1613. case CIRRUS_MMIO_BLTMODEEXT:
  1614. value = cirrus_vga_read_gr(s, 0x33);
  1615. break;
  1616. case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
  1617. value = cirrus_vga_read_gr(s, 0x34);
  1618. break;
  1619. case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
  1620. value = cirrus_vga_read_gr(s, 0x35);
  1621. break;
  1622. case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
  1623. value = cirrus_vga_read_gr(s, 0x38);
  1624. break;
  1625. case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
  1626. value = cirrus_vga_read_gr(s, 0x39);
  1627. break;
  1628. case CIRRUS_MMIO_BLTSTATUS:
  1629. value = cirrus_vga_read_gr(s, 0x31);
  1630. break;
  1631. default:
  1632. #ifdef DEBUG_CIRRUS
  1633. printf("cirrus: mmio read - address 0x%04x\n", address);
  1634. #endif
  1635. break;
  1636. }
  1637. return (uint8_t) value;
  1638. }
  1639. static void cirrus_mmio_blt_write(CirrusVGAState * s, unsigned address,
  1640. uint8_t value)
  1641. {
  1642. switch (address) {
  1643. case (CIRRUS_MMIO_BLTBGCOLOR + 0):
  1644. cirrus_vga_write_gr(s, 0x00, value);
  1645. break;
  1646. case (CIRRUS_MMIO_BLTBGCOLOR + 1):
  1647. cirrus_vga_write_gr(s, 0x10, value);
  1648. break;
  1649. case (CIRRUS_MMIO_BLTBGCOLOR + 2):
  1650. cirrus_vga_write_gr(s, 0x12, value);
  1651. break;
  1652. case (CIRRUS_MMIO_BLTBGCOLOR + 3):
  1653. cirrus_vga_write_gr(s, 0x14, value);
  1654. break;
  1655. case (CIRRUS_MMIO_BLTFGCOLOR + 0):
  1656. cirrus_vga_write_gr(s, 0x01, value);
  1657. break;
  1658. case (CIRRUS_MMIO_BLTFGCOLOR + 1):
  1659. cirrus_vga_write_gr(s, 0x11, value);
  1660. break;
  1661. case (CIRRUS_MMIO_BLTFGCOLOR + 2):
  1662. cirrus_vga_write_gr(s, 0x13, value);
  1663. break;
  1664. case (CIRRUS_MMIO_BLTFGCOLOR + 3):
  1665. cirrus_vga_write_gr(s, 0x15, value);
  1666. break;
  1667. case (CIRRUS_MMIO_BLTWIDTH + 0):
  1668. cirrus_vga_write_gr(s, 0x20, value);
  1669. break;
  1670. case (CIRRUS_MMIO_BLTWIDTH + 1):
  1671. cirrus_vga_write_gr(s, 0x21, value);
  1672. break;
  1673. case (CIRRUS_MMIO_BLTHEIGHT + 0):
  1674. cirrus_vga_write_gr(s, 0x22, value);
  1675. break;
  1676. case (CIRRUS_MMIO_BLTHEIGHT + 1):
  1677. cirrus_vga_write_gr(s, 0x23, value);
  1678. break;
  1679. case (CIRRUS_MMIO_BLTDESTPITCH + 0):
  1680. cirrus_vga_write_gr(s, 0x24, value);
  1681. break;
  1682. case (CIRRUS_MMIO_BLTDESTPITCH + 1):
  1683. cirrus_vga_write_gr(s, 0x25, value);
  1684. break;
  1685. case (CIRRUS_MMIO_BLTSRCPITCH + 0):
  1686. cirrus_vga_write_gr(s, 0x26, value);
  1687. break;
  1688. case (CIRRUS_MMIO_BLTSRCPITCH + 1):
  1689. cirrus_vga_write_gr(s, 0x27, value);
  1690. break;
  1691. case (CIRRUS_MMIO_BLTDESTADDR + 0):
  1692. cirrus_vga_write_gr(s, 0x28, value);
  1693. break;
  1694. case (CIRRUS_MMIO_BLTDESTADDR + 1):
  1695. cirrus_vga_write_gr(s, 0x29, value);
  1696. break;
  1697. case (CIRRUS_MMIO_BLTDESTADDR + 2):
  1698. cirrus_vga_write_gr(s, 0x2a, value);
  1699. break;
  1700. case (CIRRUS_MMIO_BLTDESTADDR + 3):
  1701. /* ignored */
  1702. break;
  1703. case (CIRRUS_MMIO_BLTSRCADDR + 0):
  1704. cirrus_vga_write_gr(s, 0x2c, value);
  1705. break;
  1706. case (CIRRUS_MMIO_BLTSRCADDR + 1):
  1707. cirrus_vga_write_gr(s, 0x2d, value);
  1708. break;
  1709. case (CIRRUS_MMIO_BLTSRCADDR + 2):
  1710. cirrus_vga_write_gr(s, 0x2e, value);
  1711. break;
  1712. case CIRRUS_MMIO_BLTWRITEMASK:
  1713. cirrus_vga_write_gr(s, 0x2f, value);
  1714. break;
  1715. case CIRRUS_MMIO_BLTMODE:
  1716. cirrus_vga_write_gr(s, 0x30, value);
  1717. break;
  1718. case CIRRUS_MMIO_BLTROP:
  1719. cirrus_vga_write_gr(s, 0x32, value);
  1720. break;
  1721. case CIRRUS_MMIO_BLTMODEEXT:
  1722. cirrus_vga_write_gr(s, 0x33, value);
  1723. break;
  1724. case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 0):
  1725. cirrus_vga_write_gr(s, 0x34, value);
  1726. break;
  1727. case (CIRRUS_MMIO_BLTTRANSPARENTCOLOR + 1):
  1728. cirrus_vga_write_gr(s, 0x35, value);
  1729. break;
  1730. case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 0):
  1731. cirrus_vga_write_gr(s, 0x38, value);
  1732. break;
  1733. case (CIRRUS_MMIO_BLTTRANSPARENTCOLORMASK + 1):
  1734. cirrus_vga_write_gr(s, 0x39, value);
  1735. break;
  1736. case CIRRUS_MMIO_BLTSTATUS:
  1737. cirrus_vga_write_gr(s, 0x31, value);
  1738. break;
  1739. default:
  1740. #ifdef DEBUG_CIRRUS
  1741. printf("cirrus: mmio write - addr 0x%04x val 0x%02x (ignored)\n",
  1742. address, value);
  1743. #endif
  1744. break;
  1745. }
  1746. }
  1747. /***************************************
  1748. *
  1749. * write mode 4/5
  1750. *
  1751. ***************************************/
  1752. static void cirrus_mem_writeb_mode4and5_8bpp(CirrusVGAState * s,
  1753. unsigned mode,
  1754. unsigned offset,
  1755. uint32_t mem_value)
  1756. {
  1757. int x;
  1758. unsigned val = mem_value;
  1759. uint8_t *dst;
  1760. dst = s->vga.vram_ptr + (offset &= s->cirrus_addr_mask);
  1761. for (x = 0; x < 8; x++) {
  1762. if (val & 0x80) {
  1763. *dst = s->cirrus_shadow_gr1;
  1764. } else if (mode == 5) {
  1765. *dst = s->cirrus_shadow_gr0;
  1766. }
  1767. val <<= 1;
  1768. dst++;
  1769. }
  1770. memory_region_set_dirty(&s->vga.vram, offset, 8);
  1771. }
  1772. static void cirrus_mem_writeb_mode4and5_16bpp(CirrusVGAState * s,
  1773. unsigned mode,
  1774. unsigned offset,
  1775. uint32_t mem_value)
  1776. {
  1777. int x;
  1778. unsigned val = mem_value;
  1779. uint8_t *dst;
  1780. dst = s->vga.vram_ptr + (offset &= s->cirrus_addr_mask);
  1781. for (x = 0; x < 8; x++) {
  1782. if (val & 0x80) {
  1783. *dst = s->cirrus_shadow_gr1;
  1784. *(dst + 1) = s->vga.gr[0x11];
  1785. } else if (mode == 5) {
  1786. *dst = s->cirrus_shadow_gr0;
  1787. *(dst + 1) = s->vga.gr[0x10];
  1788. }
  1789. val <<= 1;
  1790. dst += 2;
  1791. }
  1792. memory_region_set_dirty(&s->vga.vram, offset, 16);
  1793. }
  1794. /***************************************
  1795. *
  1796. * memory access between 0xa0000-0xbffff
  1797. *
  1798. ***************************************/
  1799. static uint64_t cirrus_vga_mem_read(void *opaque,
  1800. hwaddr addr,
  1801. uint32_t size)
  1802. {
  1803. CirrusVGAState *s = opaque;
  1804. unsigned bank_index;
  1805. unsigned bank_offset;
  1806. uint32_t val;
  1807. if ((s->vga.sr[0x07] & 0x01) == 0) {
  1808. return vga_mem_readb(&s->vga, addr);
  1809. }
  1810. if (addr < 0x10000) {
  1811. /* XXX handle bitblt */
  1812. /* video memory */
  1813. bank_index = addr >> 15;
  1814. bank_offset = addr & 0x7fff;
  1815. if (bank_offset < s->cirrus_bank_limit[bank_index]) {
  1816. bank_offset += s->cirrus_bank_base[bank_index];
  1817. if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
  1818. bank_offset <<= 4;
  1819. } else if (s->vga.gr[0x0B] & 0x02) {
  1820. bank_offset <<= 3;
  1821. }
  1822. bank_offset &= s->cirrus_addr_mask;
  1823. val = *(s->vga.vram_ptr + bank_offset);
  1824. } else
  1825. val = 0xff;
  1826. } else if (addr >= 0x18000 && addr < 0x18100) {
  1827. /* memory-mapped I/O */
  1828. val = 0xff;
  1829. if ((s->vga.sr[0x17] & 0x44) == 0x04) {
  1830. val = cirrus_mmio_blt_read(s, addr & 0xff);
  1831. }
  1832. } else {
  1833. val = 0xff;
  1834. #ifdef DEBUG_CIRRUS
  1835. printf("cirrus: mem_readb " TARGET_FMT_plx "\n", addr);
  1836. #endif
  1837. }
  1838. return val;
  1839. }
  1840. static void cirrus_vga_mem_write(void *opaque,
  1841. hwaddr addr,
  1842. uint64_t mem_value,
  1843. uint32_t size)
  1844. {
  1845. CirrusVGAState *s = opaque;
  1846. unsigned bank_index;
  1847. unsigned bank_offset;
  1848. unsigned mode;
  1849. if ((s->vga.sr[0x07] & 0x01) == 0) {
  1850. vga_mem_writeb(&s->vga, addr, mem_value);
  1851. return;
  1852. }
  1853. if (addr < 0x10000) {
  1854. if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
  1855. /* bitblt */
  1856. *s->cirrus_srcptr++ = (uint8_t) mem_value;
  1857. if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
  1858. cirrus_bitblt_cputovideo_next(s);
  1859. }
  1860. } else {
  1861. /* video memory */
  1862. bank_index = addr >> 15;
  1863. bank_offset = addr & 0x7fff;
  1864. if (bank_offset < s->cirrus_bank_limit[bank_index]) {
  1865. bank_offset += s->cirrus_bank_base[bank_index];
  1866. if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
  1867. bank_offset <<= 4;
  1868. } else if (s->vga.gr[0x0B] & 0x02) {
  1869. bank_offset <<= 3;
  1870. }
  1871. bank_offset &= s->cirrus_addr_mask;
  1872. mode = s->vga.gr[0x05] & 0x7;
  1873. if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
  1874. *(s->vga.vram_ptr + bank_offset) = mem_value;
  1875. memory_region_set_dirty(&s->vga.vram, bank_offset,
  1876. sizeof(mem_value));
  1877. } else {
  1878. if ((s->vga.gr[0x0B] & 0x14) != 0x14) {
  1879. cirrus_mem_writeb_mode4and5_8bpp(s, mode,
  1880. bank_offset,
  1881. mem_value);
  1882. } else {
  1883. cirrus_mem_writeb_mode4and5_16bpp(s, mode,
  1884. bank_offset,
  1885. mem_value);
  1886. }
  1887. }
  1888. }
  1889. }
  1890. } else if (addr >= 0x18000 && addr < 0x18100) {
  1891. /* memory-mapped I/O */
  1892. if ((s->vga.sr[0x17] & 0x44) == 0x04) {
  1893. cirrus_mmio_blt_write(s, addr & 0xff, mem_value);
  1894. }
  1895. } else {
  1896. #ifdef DEBUG_CIRRUS
  1897. printf("cirrus: mem_writeb " TARGET_FMT_plx " value %02x\n", addr,
  1898. mem_value);
  1899. #endif
  1900. }
  1901. }
  1902. static const MemoryRegionOps cirrus_vga_mem_ops = {
  1903. .read = cirrus_vga_mem_read,
  1904. .write = cirrus_vga_mem_write,
  1905. .endianness = DEVICE_LITTLE_ENDIAN,
  1906. .impl = {
  1907. .min_access_size = 1,
  1908. .max_access_size = 1,
  1909. },
  1910. };
  1911. /***************************************
  1912. *
  1913. * hardware cursor
  1914. *
  1915. ***************************************/
  1916. static inline void invalidate_cursor1(CirrusVGAState *s)
  1917. {
  1918. if (s->last_hw_cursor_size) {
  1919. vga_invalidate_scanlines(&s->vga,
  1920. s->last_hw_cursor_y + s->last_hw_cursor_y_start,
  1921. s->last_hw_cursor_y + s->last_hw_cursor_y_end);
  1922. }
  1923. }
  1924. static inline void cirrus_cursor_compute_yrange(CirrusVGAState *s)
  1925. {
  1926. const uint8_t *src;
  1927. uint32_t content;
  1928. int y, y_min, y_max;
  1929. src = s->vga.vram_ptr + s->real_vram_size - 16 * 1024;
  1930. if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
  1931. src += (s->vga.sr[0x13] & 0x3c) * 256;
  1932. y_min = 64;
  1933. y_max = -1;
  1934. for(y = 0; y < 64; y++) {
  1935. content = ((uint32_t *)src)[0] |
  1936. ((uint32_t *)src)[1] |
  1937. ((uint32_t *)src)[2] |
  1938. ((uint32_t *)src)[3];
  1939. if (content) {
  1940. if (y < y_min)
  1941. y_min = y;
  1942. if (y > y_max)
  1943. y_max = y;
  1944. }
  1945. src += 16;
  1946. }
  1947. } else {
  1948. src += (s->vga.sr[0x13] & 0x3f) * 256;
  1949. y_min = 32;
  1950. y_max = -1;
  1951. for(y = 0; y < 32; y++) {
  1952. content = ((uint32_t *)src)[0] |
  1953. ((uint32_t *)(src + 128))[0];
  1954. if (content) {
  1955. if (y < y_min)
  1956. y_min = y;
  1957. if (y > y_max)
  1958. y_max = y;
  1959. }
  1960. src += 4;
  1961. }
  1962. }
  1963. if (y_min > y_max) {
  1964. s->last_hw_cursor_y_start = 0;
  1965. s->last_hw_cursor_y_end = 0;
  1966. } else {
  1967. s->last_hw_cursor_y_start = y_min;
  1968. s->last_hw_cursor_y_end = y_max + 1;
  1969. }
  1970. }
  1971. /* NOTE: we do not currently handle the cursor bitmap change, so we
  1972. update the cursor only if it moves. */
  1973. static void cirrus_cursor_invalidate(VGACommonState *s1)
  1974. {
  1975. CirrusVGAState *s = container_of(s1, CirrusVGAState, vga);
  1976. int size;
  1977. if (!(s->vga.sr[0x12] & CIRRUS_CURSOR_SHOW)) {
  1978. size = 0;
  1979. } else {
  1980. if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE)
  1981. size = 64;
  1982. else
  1983. size = 32;
  1984. }
  1985. /* invalidate last cursor and new cursor if any change */
  1986. if (s->last_hw_cursor_size != size ||
  1987. s->last_hw_cursor_x != s->hw_cursor_x ||
  1988. s->last_hw_cursor_y != s->hw_cursor_y) {
  1989. invalidate_cursor1(s);
  1990. s->last_hw_cursor_size = size;
  1991. s->last_hw_cursor_x = s->hw_cursor_x;
  1992. s->last_hw_cursor_y = s->hw_cursor_y;
  1993. /* compute the real cursor min and max y */
  1994. cirrus_cursor_compute_yrange(s);
  1995. invalidate_cursor1(s);
  1996. }
  1997. }
  1998. #define DEPTH 8
  1999. #include "cirrus_vga_template.h"
  2000. #define DEPTH 16
  2001. #include "cirrus_vga_template.h"
  2002. #define DEPTH 32
  2003. #include "cirrus_vga_template.h"
  2004. static void cirrus_cursor_draw_line(VGACommonState *s1, uint8_t *d1, int scr_y)
  2005. {
  2006. CirrusVGAState *s = container_of(s1, CirrusVGAState, vga);
  2007. DisplaySurface *surface = qemu_console_surface(s->vga.con);
  2008. int w, h, bpp, x1, x2, poffset;
  2009. unsigned int color0, color1;
  2010. const uint8_t *palette, *src;
  2011. uint32_t content;
  2012. if (!(s->vga.sr[0x12] & CIRRUS_CURSOR_SHOW))
  2013. return;
  2014. /* fast test to see if the cursor intersects with the scan line */
  2015. if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
  2016. h = 64;
  2017. } else {
  2018. h = 32;
  2019. }
  2020. if (scr_y < s->hw_cursor_y ||
  2021. scr_y >= (s->hw_cursor_y + h))
  2022. return;
  2023. src = s->vga.vram_ptr + s->real_vram_size - 16 * 1024;
  2024. if (s->vga.sr[0x12] & CIRRUS_CURSOR_LARGE) {
  2025. src += (s->vga.sr[0x13] & 0x3c) * 256;
  2026. src += (scr_y - s->hw_cursor_y) * 16;
  2027. poffset = 8;
  2028. content = ((uint32_t *)src)[0] |
  2029. ((uint32_t *)src)[1] |
  2030. ((uint32_t *)src)[2] |
  2031. ((uint32_t *)src)[3];
  2032. } else {
  2033. src += (s->vga.sr[0x13] & 0x3f) * 256;
  2034. src += (scr_y - s->hw_cursor_y) * 4;
  2035. poffset = 128;
  2036. content = ((uint32_t *)src)[0] |
  2037. ((uint32_t *)(src + 128))[0];
  2038. }
  2039. /* if nothing to draw, no need to continue */
  2040. if (!content)
  2041. return;
  2042. w = h;
  2043. x1 = s->hw_cursor_x;
  2044. if (x1 >= s->vga.last_scr_width)
  2045. return;
  2046. x2 = s->hw_cursor_x + w;
  2047. if (x2 > s->vga.last_scr_width)
  2048. x2 = s->vga.last_scr_width;
  2049. w = x2 - x1;
  2050. palette = s->cirrus_hidden_palette;
  2051. color0 = s->vga.rgb_to_pixel(c6_to_8(palette[0x0 * 3]),
  2052. c6_to_8(palette[0x0 * 3 + 1]),
  2053. c6_to_8(palette[0x0 * 3 + 2]));
  2054. color1 = s->vga.rgb_to_pixel(c6_to_8(palette[0xf * 3]),
  2055. c6_to_8(palette[0xf * 3 + 1]),
  2056. c6_to_8(palette[0xf * 3 + 2]));
  2057. bpp = surface_bytes_per_pixel(surface);
  2058. d1 += x1 * bpp;
  2059. switch (surface_bits_per_pixel(surface)) {
  2060. default:
  2061. break;
  2062. case 8:
  2063. vga_draw_cursor_line_8(d1, src, poffset, w, color0, color1, 0xff);
  2064. break;
  2065. case 15:
  2066. vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0x7fff);
  2067. break;
  2068. case 16:
  2069. vga_draw_cursor_line_16(d1, src, poffset, w, color0, color1, 0xffff);
  2070. break;
  2071. case 32:
  2072. vga_draw_cursor_line_32(d1, src, poffset, w, color0, color1, 0xffffff);
  2073. break;
  2074. }
  2075. }
  2076. /***************************************
  2077. *
  2078. * LFB memory access
  2079. *
  2080. ***************************************/
  2081. static uint64_t cirrus_linear_read(void *opaque, hwaddr addr,
  2082. unsigned size)
  2083. {
  2084. CirrusVGAState *s = opaque;
  2085. uint32_t ret;
  2086. addr &= s->cirrus_addr_mask;
  2087. if (((s->vga.sr[0x17] & 0x44) == 0x44) &&
  2088. ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
  2089. /* memory-mapped I/O */
  2090. ret = cirrus_mmio_blt_read(s, addr & 0xff);
  2091. } else if (0) {
  2092. /* XXX handle bitblt */
  2093. ret = 0xff;
  2094. } else {
  2095. /* video memory */
  2096. if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
  2097. addr <<= 4;
  2098. } else if (s->vga.gr[0x0B] & 0x02) {
  2099. addr <<= 3;
  2100. }
  2101. addr &= s->cirrus_addr_mask;
  2102. ret = *(s->vga.vram_ptr + addr);
  2103. }
  2104. return ret;
  2105. }
  2106. static void cirrus_linear_write(void *opaque, hwaddr addr,
  2107. uint64_t val, unsigned size)
  2108. {
  2109. CirrusVGAState *s = opaque;
  2110. unsigned mode;
  2111. addr &= s->cirrus_addr_mask;
  2112. if (((s->vga.sr[0x17] & 0x44) == 0x44) &&
  2113. ((addr & s->linear_mmio_mask) == s->linear_mmio_mask)) {
  2114. /* memory-mapped I/O */
  2115. cirrus_mmio_blt_write(s, addr & 0xff, val);
  2116. } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
  2117. /* bitblt */
  2118. *s->cirrus_srcptr++ = (uint8_t) val;
  2119. if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
  2120. cirrus_bitblt_cputovideo_next(s);
  2121. }
  2122. } else {
  2123. /* video memory */
  2124. if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
  2125. addr <<= 4;
  2126. } else if (s->vga.gr[0x0B] & 0x02) {
  2127. addr <<= 3;
  2128. }
  2129. addr &= s->cirrus_addr_mask;
  2130. mode = s->vga.gr[0x05] & 0x7;
  2131. if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
  2132. *(s->vga.vram_ptr + addr) = (uint8_t) val;
  2133. memory_region_set_dirty(&s->vga.vram, addr, 1);
  2134. } else {
  2135. if ((s->vga.gr[0x0B] & 0x14) != 0x14) {
  2136. cirrus_mem_writeb_mode4and5_8bpp(s, mode, addr, val);
  2137. } else {
  2138. cirrus_mem_writeb_mode4and5_16bpp(s, mode, addr, val);
  2139. }
  2140. }
  2141. }
  2142. }
  2143. /***************************************
  2144. *
  2145. * system to screen memory access
  2146. *
  2147. ***************************************/
  2148. static uint64_t cirrus_linear_bitblt_read(void *opaque,
  2149. hwaddr addr,
  2150. unsigned size)
  2151. {
  2152. CirrusVGAState *s = opaque;
  2153. uint32_t ret;
  2154. /* XXX handle bitblt */
  2155. (void)s;
  2156. ret = 0xff;
  2157. return ret;
  2158. }
  2159. static void cirrus_linear_bitblt_write(void *opaque,
  2160. hwaddr addr,
  2161. uint64_t val,
  2162. unsigned size)
  2163. {
  2164. CirrusVGAState *s = opaque;
  2165. if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
  2166. /* bitblt */
  2167. *s->cirrus_srcptr++ = (uint8_t) val;
  2168. if (s->cirrus_srcptr >= s->cirrus_srcptr_end) {
  2169. cirrus_bitblt_cputovideo_next(s);
  2170. }
  2171. }
  2172. }
  2173. static const MemoryRegionOps cirrus_linear_bitblt_io_ops = {
  2174. .read = cirrus_linear_bitblt_read,
  2175. .write = cirrus_linear_bitblt_write,
  2176. .endianness = DEVICE_LITTLE_ENDIAN,
  2177. .impl = {
  2178. .min_access_size = 1,
  2179. .max_access_size = 1,
  2180. },
  2181. };
  2182. static void map_linear_vram_bank(CirrusVGAState *s, unsigned bank)
  2183. {
  2184. MemoryRegion *mr = &s->cirrus_bank[bank];
  2185. bool enabled = !(s->cirrus_srcptr != s->cirrus_srcptr_end)
  2186. && !((s->vga.sr[0x07] & 0x01) == 0)
  2187. && !((s->vga.gr[0x0B] & 0x14) == 0x14)
  2188. && !(s->vga.gr[0x0B] & 0x02);
  2189. memory_region_set_enabled(mr, enabled);
  2190. memory_region_set_alias_offset(mr, s->cirrus_bank_base[bank]);
  2191. }
  2192. static void map_linear_vram(CirrusVGAState *s)
  2193. {
  2194. if (s->bustype == CIRRUS_BUSTYPE_PCI && !s->linear_vram) {
  2195. s->linear_vram = true;
  2196. memory_region_add_subregion_overlap(&s->pci_bar, 0, &s->vga.vram, 1);
  2197. }
  2198. map_linear_vram_bank(s, 0);
  2199. map_linear_vram_bank(s, 1);
  2200. }
  2201. static void unmap_linear_vram(CirrusVGAState *s)
  2202. {
  2203. if (s->bustype == CIRRUS_BUSTYPE_PCI && s->linear_vram) {
  2204. s->linear_vram = false;
  2205. memory_region_del_subregion(&s->pci_bar, &s->vga.vram);
  2206. }
  2207. memory_region_set_enabled(&s->cirrus_bank[0], false);
  2208. memory_region_set_enabled(&s->cirrus_bank[1], false);
  2209. }
  2210. /* Compute the memory access functions */
  2211. static void cirrus_update_memory_access(CirrusVGAState *s)
  2212. {
  2213. unsigned mode;
  2214. memory_region_transaction_begin();
  2215. if ((s->vga.sr[0x17] & 0x44) == 0x44) {
  2216. goto generic_io;
  2217. } else if (s->cirrus_srcptr != s->cirrus_srcptr_end) {
  2218. goto generic_io;
  2219. } else {
  2220. if ((s->vga.gr[0x0B] & 0x14) == 0x14) {
  2221. goto generic_io;
  2222. } else if (s->vga.gr[0x0B] & 0x02) {
  2223. goto generic_io;
  2224. }
  2225. mode = s->vga.gr[0x05] & 0x7;
  2226. if (mode < 4 || mode > 5 || ((s->vga.gr[0x0B] & 0x4) == 0)) {
  2227. map_linear_vram(s);
  2228. } else {
  2229. generic_io:
  2230. unmap_linear_vram(s);
  2231. }
  2232. }
  2233. memory_region_transaction_commit();
  2234. }
  2235. /* I/O ports */
  2236. static uint64_t cirrus_vga_ioport_read(void *opaque, hwaddr addr,
  2237. unsigned size)
  2238. {
  2239. CirrusVGAState *c = opaque;
  2240. VGACommonState *s = &c->vga;
  2241. int val, index;
  2242. addr += 0x3b0;
  2243. if (vga_ioport_invalid(s, addr)) {
  2244. val = 0xff;
  2245. } else {
  2246. switch (addr) {
  2247. case 0x3c0:
  2248. if (s->ar_flip_flop == 0) {
  2249. val = s->ar_index;
  2250. } else {
  2251. val = 0;
  2252. }
  2253. break;
  2254. case 0x3c1:
  2255. index = s->ar_index & 0x1f;
  2256. if (index < 21)
  2257. val = s->ar[index];
  2258. else
  2259. val = 0;
  2260. break;
  2261. case 0x3c2:
  2262. val = s->st00;
  2263. break;
  2264. case 0x3c4:
  2265. val = s->sr_index;
  2266. break;
  2267. case 0x3c5:
  2268. val = cirrus_vga_read_sr(c);
  2269. break;
  2270. #ifdef DEBUG_VGA_REG
  2271. printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
  2272. #endif
  2273. break;
  2274. case 0x3c6:
  2275. val = cirrus_read_hidden_dac(c);
  2276. break;
  2277. case 0x3c7:
  2278. val = s->dac_state;
  2279. break;
  2280. case 0x3c8:
  2281. val = s->dac_write_index;
  2282. c->cirrus_hidden_dac_lockindex = 0;
  2283. break;
  2284. case 0x3c9:
  2285. val = cirrus_vga_read_palette(c);
  2286. break;
  2287. case 0x3ca:
  2288. val = s->fcr;
  2289. break;
  2290. case 0x3cc:
  2291. val = s->msr;
  2292. break;
  2293. case 0x3ce:
  2294. val = s->gr_index;
  2295. break;
  2296. case 0x3cf:
  2297. val = cirrus_vga_read_gr(c, s->gr_index);
  2298. #ifdef DEBUG_VGA_REG
  2299. printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
  2300. #endif
  2301. break;
  2302. case 0x3b4:
  2303. case 0x3d4:
  2304. val = s->cr_index;
  2305. break;
  2306. case 0x3b5:
  2307. case 0x3d5:
  2308. val = cirrus_vga_read_cr(c, s->cr_index);
  2309. #ifdef DEBUG_VGA_REG
  2310. printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
  2311. #endif
  2312. break;
  2313. case 0x3ba:
  2314. case 0x3da:
  2315. /* just toggle to fool polling */
  2316. val = s->st01 = s->retrace(s);
  2317. s->ar_flip_flop = 0;
  2318. break;
  2319. default:
  2320. val = 0x00;
  2321. break;
  2322. }
  2323. }
  2324. #if defined(DEBUG_VGA)
  2325. printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
  2326. #endif
  2327. return val;
  2328. }
  2329. static void cirrus_vga_ioport_write(void *opaque, hwaddr addr, uint64_t val,
  2330. unsigned size)
  2331. {
  2332. CirrusVGAState *c = opaque;
  2333. VGACommonState *s = &c->vga;
  2334. int index;
  2335. addr += 0x3b0;
  2336. /* check port range access depending on color/monochrome mode */
  2337. if (vga_ioport_invalid(s, addr)) {
  2338. return;
  2339. }
  2340. #ifdef DEBUG_VGA
  2341. printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
  2342. #endif
  2343. switch (addr) {
  2344. case 0x3c0:
  2345. if (s->ar_flip_flop == 0) {
  2346. val &= 0x3f;
  2347. s->ar_index = val;
  2348. } else {
  2349. index = s->ar_index & 0x1f;
  2350. switch (index) {
  2351. case 0x00 ... 0x0f:
  2352. s->ar[index] = val & 0x3f;
  2353. break;
  2354. case 0x10:
  2355. s->ar[index] = val & ~0x10;
  2356. break;
  2357. case 0x11:
  2358. s->ar[index] = val;
  2359. break;
  2360. case 0x12:
  2361. s->ar[index] = val & ~0xc0;
  2362. break;
  2363. case 0x13:
  2364. s->ar[index] = val & ~0xf0;
  2365. break;
  2366. case 0x14:
  2367. s->ar[index] = val & ~0xf0;
  2368. break;
  2369. default:
  2370. break;
  2371. }
  2372. }
  2373. s->ar_flip_flop ^= 1;
  2374. break;
  2375. case 0x3c2:
  2376. s->msr = val & ~0x10;
  2377. s->update_retrace_info(s);
  2378. break;
  2379. case 0x3c4:
  2380. s->sr_index = val;
  2381. break;
  2382. case 0x3c5:
  2383. #ifdef DEBUG_VGA_REG
  2384. printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
  2385. #endif
  2386. cirrus_vga_write_sr(c, val);
  2387. break;
  2388. case 0x3c6:
  2389. cirrus_write_hidden_dac(c, val);
  2390. break;
  2391. case 0x3c7:
  2392. s->dac_read_index = val;
  2393. s->dac_sub_index = 0;
  2394. s->dac_state = 3;
  2395. break;
  2396. case 0x3c8:
  2397. s->dac_write_index = val;
  2398. s->dac_sub_index = 0;
  2399. s->dac_state = 0;
  2400. break;
  2401. case 0x3c9:
  2402. cirrus_vga_write_palette(c, val);
  2403. break;
  2404. case 0x3ce:
  2405. s->gr_index = val;
  2406. break;
  2407. case 0x3cf:
  2408. #ifdef DEBUG_VGA_REG
  2409. printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
  2410. #endif
  2411. cirrus_vga_write_gr(c, s->gr_index, val);
  2412. break;
  2413. case 0x3b4:
  2414. case 0x3d4:
  2415. s->cr_index = val;
  2416. break;
  2417. case 0x3b5:
  2418. case 0x3d5:
  2419. #ifdef DEBUG_VGA_REG
  2420. printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
  2421. #endif
  2422. cirrus_vga_write_cr(c, val);
  2423. break;
  2424. case 0x3ba:
  2425. case 0x3da:
  2426. s->fcr = val & 0x10;
  2427. break;
  2428. }
  2429. }
  2430. /***************************************
  2431. *
  2432. * memory-mapped I/O access
  2433. *
  2434. ***************************************/
  2435. static uint64_t cirrus_mmio_read(void *opaque, hwaddr addr,
  2436. unsigned size)
  2437. {
  2438. CirrusVGAState *s = opaque;
  2439. if (addr >= 0x100) {
  2440. return cirrus_mmio_blt_read(s, addr - 0x100);
  2441. } else {
  2442. return cirrus_vga_ioport_read(s, addr + 0x10, size);
  2443. }
  2444. }
  2445. static void cirrus_mmio_write(void *opaque, hwaddr addr,
  2446. uint64_t val, unsigned size)
  2447. {
  2448. CirrusVGAState *s = opaque;
  2449. if (addr >= 0x100) {
  2450. cirrus_mmio_blt_write(s, addr - 0x100, val);
  2451. } else {
  2452. cirrus_vga_ioport_write(s, addr + 0x10, val, size);
  2453. }
  2454. }
  2455. static const MemoryRegionOps cirrus_mmio_io_ops = {
  2456. .read = cirrus_mmio_read,
  2457. .write = cirrus_mmio_write,
  2458. .endianness = DEVICE_LITTLE_ENDIAN,
  2459. .impl = {
  2460. .min_access_size = 1,
  2461. .max_access_size = 1,
  2462. },
  2463. };
  2464. /* load/save state */
  2465. static int cirrus_post_load(void *opaque, int version_id)
  2466. {
  2467. CirrusVGAState *s = opaque;
  2468. s->vga.gr[0x00] = s->cirrus_shadow_gr0 & 0x0f;
  2469. s->vga.gr[0x01] = s->cirrus_shadow_gr1 & 0x0f;
  2470. cirrus_update_memory_access(s);
  2471. /* force refresh */
  2472. s->vga.graphic_mode = -1;
  2473. cirrus_update_bank_ptr(s, 0);
  2474. cirrus_update_bank_ptr(s, 1);
  2475. return 0;
  2476. }
  2477. static const VMStateDescription vmstate_cirrus_vga = {
  2478. .name = "cirrus_vga",
  2479. .version_id = 2,
  2480. .minimum_version_id = 1,
  2481. .minimum_version_id_old = 1,
  2482. .post_load = cirrus_post_load,
  2483. .fields = (VMStateField []) {
  2484. VMSTATE_UINT32(vga.latch, CirrusVGAState),
  2485. VMSTATE_UINT8(vga.sr_index, CirrusVGAState),
  2486. VMSTATE_BUFFER(vga.sr, CirrusVGAState),
  2487. VMSTATE_UINT8(vga.gr_index, CirrusVGAState),
  2488. VMSTATE_UINT8(cirrus_shadow_gr0, CirrusVGAState),
  2489. VMSTATE_UINT8(cirrus_shadow_gr1, CirrusVGAState),
  2490. VMSTATE_BUFFER_START_MIDDLE(vga.gr, CirrusVGAState, 2),
  2491. VMSTATE_UINT8(vga.ar_index, CirrusVGAState),
  2492. VMSTATE_BUFFER(vga.ar, CirrusVGAState),
  2493. VMSTATE_INT32(vga.ar_flip_flop, CirrusVGAState),
  2494. VMSTATE_UINT8(vga.cr_index, CirrusVGAState),
  2495. VMSTATE_BUFFER(vga.cr, CirrusVGAState),
  2496. VMSTATE_UINT8(vga.msr, CirrusVGAState),
  2497. VMSTATE_UINT8(vga.fcr, CirrusVGAState),
  2498. VMSTATE_UINT8(vga.st00, CirrusVGAState),
  2499. VMSTATE_UINT8(vga.st01, CirrusVGAState),
  2500. VMSTATE_UINT8(vga.dac_state, CirrusVGAState),
  2501. VMSTATE_UINT8(vga.dac_sub_index, CirrusVGAState),
  2502. VMSTATE_UINT8(vga.dac_read_index, CirrusVGAState),
  2503. VMSTATE_UINT8(vga.dac_write_index, CirrusVGAState),
  2504. VMSTATE_BUFFER(vga.dac_cache, CirrusVGAState),
  2505. VMSTATE_BUFFER(vga.palette, CirrusVGAState),
  2506. VMSTATE_INT32(vga.bank_offset, CirrusVGAState),
  2507. VMSTATE_UINT8(cirrus_hidden_dac_lockindex, CirrusVGAState),
  2508. VMSTATE_UINT8(cirrus_hidden_dac_data, CirrusVGAState),
  2509. VMSTATE_UINT32(hw_cursor_x, CirrusVGAState),
  2510. VMSTATE_UINT32(hw_cursor_y, CirrusVGAState),
  2511. /* XXX: we do not save the bitblt state - we assume we do not save
  2512. the state when the blitter is active */
  2513. VMSTATE_END_OF_LIST()
  2514. }
  2515. };
  2516. static const VMStateDescription vmstate_pci_cirrus_vga = {
  2517. .name = "cirrus_vga",
  2518. .version_id = 2,
  2519. .minimum_version_id = 2,
  2520. .minimum_version_id_old = 2,
  2521. .fields = (VMStateField []) {
  2522. VMSTATE_PCI_DEVICE(dev, PCICirrusVGAState),
  2523. VMSTATE_STRUCT(cirrus_vga, PCICirrusVGAState, 0,
  2524. vmstate_cirrus_vga, CirrusVGAState),
  2525. VMSTATE_END_OF_LIST()
  2526. }
  2527. };
  2528. /***************************************
  2529. *
  2530. * initialize
  2531. *
  2532. ***************************************/
  2533. static void cirrus_reset(void *opaque)
  2534. {
  2535. CirrusVGAState *s = opaque;
  2536. vga_common_reset(&s->vga);
  2537. unmap_linear_vram(s);
  2538. s->vga.sr[0x06] = 0x0f;
  2539. if (s->device_id == CIRRUS_ID_CLGD5446) {
  2540. /* 4MB 64 bit memory config, always PCI */
  2541. s->vga.sr[0x1F] = 0x2d; // MemClock
  2542. s->vga.gr[0x18] = 0x0f; // fastest memory configuration
  2543. s->vga.sr[0x0f] = 0x98;
  2544. s->vga.sr[0x17] = 0x20;
  2545. s->vga.sr[0x15] = 0x04; /* memory size, 3=2MB, 4=4MB */
  2546. } else {
  2547. s->vga.sr[0x1F] = 0x22; // MemClock
  2548. s->vga.sr[0x0F] = CIRRUS_MEMSIZE_2M;
  2549. s->vga.sr[0x17] = s->bustype;
  2550. s->vga.sr[0x15] = 0x03; /* memory size, 3=2MB, 4=4MB */
  2551. }
  2552. s->vga.cr[0x27] = s->device_id;
  2553. s->cirrus_hidden_dac_lockindex = 5;
  2554. s->cirrus_hidden_dac_data = 0;
  2555. }
  2556. static const MemoryRegionOps cirrus_linear_io_ops = {
  2557. .read = cirrus_linear_read,
  2558. .write = cirrus_linear_write,
  2559. .endianness = DEVICE_LITTLE_ENDIAN,
  2560. .impl = {
  2561. .min_access_size = 1,
  2562. .max_access_size = 1,
  2563. },
  2564. };
  2565. static const MemoryRegionOps cirrus_vga_io_ops = {
  2566. .read = cirrus_vga_ioport_read,
  2567. .write = cirrus_vga_ioport_write,
  2568. .endianness = DEVICE_LITTLE_ENDIAN,
  2569. .impl = {
  2570. .min_access_size = 1,
  2571. .max_access_size = 1,
  2572. },
  2573. };
  2574. static void cirrus_init_common(CirrusVGAState *s, Object *owner,
  2575. int device_id, int is_pci,
  2576. MemoryRegion *system_memory,
  2577. MemoryRegion *system_io)
  2578. {
  2579. int i;
  2580. static int inited;
  2581. if (!inited) {
  2582. inited = 1;
  2583. for(i = 0;i < 256; i++)
  2584. rop_to_index[i] = CIRRUS_ROP_NOP_INDEX; /* nop rop */
  2585. rop_to_index[CIRRUS_ROP_0] = 0;
  2586. rop_to_index[CIRRUS_ROP_SRC_AND_DST] = 1;
  2587. rop_to_index[CIRRUS_ROP_NOP] = 2;
  2588. rop_to_index[CIRRUS_ROP_SRC_AND_NOTDST] = 3;
  2589. rop_to_index[CIRRUS_ROP_NOTDST] = 4;
  2590. rop_to_index[CIRRUS_ROP_SRC] = 5;
  2591. rop_to_index[CIRRUS_ROP_1] = 6;
  2592. rop_to_index[CIRRUS_ROP_NOTSRC_AND_DST] = 7;
  2593. rop_to_index[CIRRUS_ROP_SRC_XOR_DST] = 8;
  2594. rop_to_index[CIRRUS_ROP_SRC_OR_DST] = 9;
  2595. rop_to_index[CIRRUS_ROP_NOTSRC_OR_NOTDST] = 10;
  2596. rop_to_index[CIRRUS_ROP_SRC_NOTXOR_DST] = 11;
  2597. rop_to_index[CIRRUS_ROP_SRC_OR_NOTDST] = 12;
  2598. rop_to_index[CIRRUS_ROP_NOTSRC] = 13;
  2599. rop_to_index[CIRRUS_ROP_NOTSRC_OR_DST] = 14;
  2600. rop_to_index[CIRRUS_ROP_NOTSRC_AND_NOTDST] = 15;
  2601. s->device_id = device_id;
  2602. if (is_pci)
  2603. s->bustype = CIRRUS_BUSTYPE_PCI;
  2604. else
  2605. s->bustype = CIRRUS_BUSTYPE_ISA;
  2606. }
  2607. /* Register ioport 0x3b0 - 0x3df */
  2608. memory_region_init_io(&s->cirrus_vga_io, owner, &cirrus_vga_io_ops, s,
  2609. "cirrus-io", 0x30);
  2610. memory_region_set_flush_coalesced(&s->cirrus_vga_io);
  2611. memory_region_add_subregion(system_io, 0x3b0, &s->cirrus_vga_io);
  2612. memory_region_init(&s->low_mem_container, owner,
  2613. "cirrus-lowmem-container",
  2614. 0x20000);
  2615. memory_region_init_io(&s->low_mem, owner, &cirrus_vga_mem_ops, s,
  2616. "cirrus-low-memory", 0x20000);
  2617. memory_region_add_subregion(&s->low_mem_container, 0, &s->low_mem);
  2618. for (i = 0; i < 2; ++i) {
  2619. static const char *names[] = { "vga.bank0", "vga.bank1" };
  2620. MemoryRegion *bank = &s->cirrus_bank[i];
  2621. memory_region_init_alias(bank, owner, names[i], &s->vga.vram,
  2622. 0, 0x8000);
  2623. memory_region_set_enabled(bank, false);
  2624. memory_region_add_subregion_overlap(&s->low_mem_container, i * 0x8000,
  2625. bank, 1);
  2626. }
  2627. memory_region_add_subregion_overlap(system_memory,
  2628. isa_mem_base + 0x000a0000,
  2629. &s->low_mem_container,
  2630. 1);
  2631. memory_region_set_coalescing(&s->low_mem);
  2632. /* I/O handler for LFB */
  2633. memory_region_init_io(&s->cirrus_linear_io, owner, &cirrus_linear_io_ops, s,
  2634. "cirrus-linear-io", s->vga.vram_size_mb
  2635. * 1024 * 1024);
  2636. memory_region_set_flush_coalesced(&s->cirrus_linear_io);
  2637. /* I/O handler for LFB */
  2638. memory_region_init_io(&s->cirrus_linear_bitblt_io, owner,
  2639. &cirrus_linear_bitblt_io_ops,
  2640. s,
  2641. "cirrus-bitblt-mmio",
  2642. 0x400000);
  2643. memory_region_set_flush_coalesced(&s->cirrus_linear_bitblt_io);
  2644. /* I/O handler for memory-mapped I/O */
  2645. memory_region_init_io(&s->cirrus_mmio_io, owner, &cirrus_mmio_io_ops, s,
  2646. "cirrus-mmio", CIRRUS_PNPMMIO_SIZE);
  2647. memory_region_set_flush_coalesced(&s->cirrus_mmio_io);
  2648. s->real_vram_size =
  2649. (s->device_id == CIRRUS_ID_CLGD5446) ? 4096 * 1024 : 2048 * 1024;
  2650. /* XXX: s->vga.vram_size must be a power of two */
  2651. s->cirrus_addr_mask = s->real_vram_size - 1;
  2652. s->linear_mmio_mask = s->real_vram_size - 256;
  2653. s->vga.get_bpp = cirrus_get_bpp;
  2654. s->vga.get_offsets = cirrus_get_offsets;
  2655. s->vga.get_resolution = cirrus_get_resolution;
  2656. s->vga.cursor_invalidate = cirrus_cursor_invalidate;
  2657. s->vga.cursor_draw_line = cirrus_cursor_draw_line;
  2658. qemu_register_reset(cirrus_reset, s);
  2659. }
  2660. /***************************************
  2661. *
  2662. * ISA bus support
  2663. *
  2664. ***************************************/
  2665. static void isa_cirrus_vga_realizefn(DeviceState *dev, Error **errp)
  2666. {
  2667. ISADevice *isadev = ISA_DEVICE(dev);
  2668. ISACirrusVGAState *d = ISA_CIRRUS_VGA(dev);
  2669. VGACommonState *s = &d->cirrus_vga.vga;
  2670. vga_common_init(s, OBJECT(dev));
  2671. cirrus_init_common(&d->cirrus_vga, OBJECT(dev), CIRRUS_ID_CLGD5430, 0,
  2672. isa_address_space(isadev),
  2673. isa_address_space_io(isadev));
  2674. s->con = graphic_console_init(dev, 0, s->hw_ops, s);
  2675. rom_add_vga(VGABIOS_CIRRUS_FILENAME);
  2676. /* XXX ISA-LFB support */
  2677. /* FIXME not qdev yet */
  2678. }
  2679. static Property isa_cirrus_vga_properties[] = {
  2680. DEFINE_PROP_UINT32("vgamem_mb", struct ISACirrusVGAState,
  2681. cirrus_vga.vga.vram_size_mb, 8),
  2682. DEFINE_PROP_END_OF_LIST(),
  2683. };
  2684. static void isa_cirrus_vga_class_init(ObjectClass *klass, void *data)
  2685. {
  2686. DeviceClass *dc = DEVICE_CLASS(klass);
  2687. dc->vmsd = &vmstate_cirrus_vga;
  2688. dc->realize = isa_cirrus_vga_realizefn;
  2689. dc->props = isa_cirrus_vga_properties;
  2690. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  2691. }
  2692. static const TypeInfo isa_cirrus_vga_info = {
  2693. .name = TYPE_ISA_CIRRUS_VGA,
  2694. .parent = TYPE_ISA_DEVICE,
  2695. .instance_size = sizeof(ISACirrusVGAState),
  2696. .class_init = isa_cirrus_vga_class_init,
  2697. };
  2698. /***************************************
  2699. *
  2700. * PCI bus support
  2701. *
  2702. ***************************************/
  2703. static int pci_cirrus_vga_initfn(PCIDevice *dev)
  2704. {
  2705. PCICirrusVGAState *d = DO_UPCAST(PCICirrusVGAState, dev, dev);
  2706. CirrusVGAState *s = &d->cirrus_vga;
  2707. PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
  2708. int16_t device_id = pc->device_id;
  2709. /* setup VGA */
  2710. vga_common_init(&s->vga, OBJECT(dev));
  2711. cirrus_init_common(s, OBJECT(dev), device_id, 1, pci_address_space(dev),
  2712. pci_address_space_io(dev));
  2713. s->vga.con = graphic_console_init(DEVICE(dev), 0, s->vga.hw_ops, &s->vga);
  2714. /* setup PCI */
  2715. memory_region_init(&s->pci_bar, OBJECT(dev), "cirrus-pci-bar0", 0x2000000);
  2716. /* XXX: add byte swapping apertures */
  2717. memory_region_add_subregion(&s->pci_bar, 0, &s->cirrus_linear_io);
  2718. memory_region_add_subregion(&s->pci_bar, 0x1000000,
  2719. &s->cirrus_linear_bitblt_io);
  2720. /* setup memory space */
  2721. /* memory #0 LFB */
  2722. /* memory #1 memory-mapped I/O */
  2723. /* XXX: s->vga.vram_size must be a power of two */
  2724. pci_register_bar(&d->dev, 0, PCI_BASE_ADDRESS_MEM_PREFETCH, &s->pci_bar);
  2725. if (device_id == CIRRUS_ID_CLGD5446) {
  2726. pci_register_bar(&d->dev, 1, 0, &s->cirrus_mmio_io);
  2727. }
  2728. return 0;
  2729. }
  2730. static Property pci_vga_cirrus_properties[] = {
  2731. DEFINE_PROP_UINT32("vgamem_mb", struct PCICirrusVGAState,
  2732. cirrus_vga.vga.vram_size_mb, 8),
  2733. DEFINE_PROP_END_OF_LIST(),
  2734. };
  2735. static void cirrus_vga_class_init(ObjectClass *klass, void *data)
  2736. {
  2737. DeviceClass *dc = DEVICE_CLASS(klass);
  2738. PCIDeviceClass *k = PCI_DEVICE_CLASS(klass);
  2739. k->init = pci_cirrus_vga_initfn;
  2740. k->romfile = VGABIOS_CIRRUS_FILENAME;
  2741. k->vendor_id = PCI_VENDOR_ID_CIRRUS;
  2742. k->device_id = CIRRUS_ID_CLGD5446;
  2743. k->class_id = PCI_CLASS_DISPLAY_VGA;
  2744. set_bit(DEVICE_CATEGORY_DISPLAY, dc->categories);
  2745. dc->desc = "Cirrus CLGD 54xx VGA";
  2746. dc->vmsd = &vmstate_pci_cirrus_vga;
  2747. dc->props = pci_vga_cirrus_properties;
  2748. dc->hotpluggable = false;
  2749. }
  2750. static const TypeInfo cirrus_vga_info = {
  2751. .name = "cirrus-vga",
  2752. .parent = TYPE_PCI_DEVICE,
  2753. .instance_size = sizeof(PCICirrusVGAState),
  2754. .class_init = cirrus_vga_class_init,
  2755. };
  2756. static void cirrus_vga_register_types(void)
  2757. {
  2758. type_register_static(&isa_cirrus_vga_info);
  2759. type_register_static(&cirrus_vga_info);
  2760. }
  2761. type_init(cirrus_vga_register_types)