cirrus_vga.c 90 KB

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