cirrus_vga.c 100 KB

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