cirrus_vga.c 90 KB

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