2
0

vga.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418
  1. /*
  2. * QEMU VGA Emulator.
  3. *
  4. * Copyright (c) 2003 Fabrice Bellard
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "hw.h"
  25. #include "console.h"
  26. #include "pc.h"
  27. #include "pci.h"
  28. #include "vga_int.h"
  29. #include "pixel_ops.h"
  30. #include "qemu-timer.h"
  31. //#define DEBUG_VGA
  32. //#define DEBUG_VGA_MEM
  33. //#define DEBUG_VGA_REG
  34. //#define DEBUG_BOCHS_VBE
  35. /* force some bits to zero */
  36. const uint8_t sr_mask[8] = {
  37. 0x03,
  38. 0x3d,
  39. 0x0f,
  40. 0x3f,
  41. 0x0e,
  42. 0x00,
  43. 0x00,
  44. 0xff,
  45. };
  46. const uint8_t gr_mask[16] = {
  47. 0x0f, /* 0x00 */
  48. 0x0f, /* 0x01 */
  49. 0x0f, /* 0x02 */
  50. 0x1f, /* 0x03 */
  51. 0x03, /* 0x04 */
  52. 0x7b, /* 0x05 */
  53. 0x0f, /* 0x06 */
  54. 0x0f, /* 0x07 */
  55. 0xff, /* 0x08 */
  56. 0x00, /* 0x09 */
  57. 0x00, /* 0x0a */
  58. 0x00, /* 0x0b */
  59. 0x00, /* 0x0c */
  60. 0x00, /* 0x0d */
  61. 0x00, /* 0x0e */
  62. 0x00, /* 0x0f */
  63. };
  64. #define cbswap_32(__x) \
  65. ((uint32_t)( \
  66. (((uint32_t)(__x) & (uint32_t)0x000000ffUL) << 24) | \
  67. (((uint32_t)(__x) & (uint32_t)0x0000ff00UL) << 8) | \
  68. (((uint32_t)(__x) & (uint32_t)0x00ff0000UL) >> 8) | \
  69. (((uint32_t)(__x) & (uint32_t)0xff000000UL) >> 24) ))
  70. #ifdef HOST_WORDS_BIGENDIAN
  71. #define PAT(x) cbswap_32(x)
  72. #else
  73. #define PAT(x) (x)
  74. #endif
  75. #ifdef HOST_WORDS_BIGENDIAN
  76. #define BIG 1
  77. #else
  78. #define BIG 0
  79. #endif
  80. #ifdef HOST_WORDS_BIGENDIAN
  81. #define GET_PLANE(data, p) (((data) >> (24 - (p) * 8)) & 0xff)
  82. #else
  83. #define GET_PLANE(data, p) (((data) >> ((p) * 8)) & 0xff)
  84. #endif
  85. static const uint32_t mask16[16] = {
  86. PAT(0x00000000),
  87. PAT(0x000000ff),
  88. PAT(0x0000ff00),
  89. PAT(0x0000ffff),
  90. PAT(0x00ff0000),
  91. PAT(0x00ff00ff),
  92. PAT(0x00ffff00),
  93. PAT(0x00ffffff),
  94. PAT(0xff000000),
  95. PAT(0xff0000ff),
  96. PAT(0xff00ff00),
  97. PAT(0xff00ffff),
  98. PAT(0xffff0000),
  99. PAT(0xffff00ff),
  100. PAT(0xffffff00),
  101. PAT(0xffffffff),
  102. };
  103. #undef PAT
  104. #ifdef HOST_WORDS_BIGENDIAN
  105. #define PAT(x) (x)
  106. #else
  107. #define PAT(x) cbswap_32(x)
  108. #endif
  109. static const uint32_t dmask16[16] = {
  110. PAT(0x00000000),
  111. PAT(0x000000ff),
  112. PAT(0x0000ff00),
  113. PAT(0x0000ffff),
  114. PAT(0x00ff0000),
  115. PAT(0x00ff00ff),
  116. PAT(0x00ffff00),
  117. PAT(0x00ffffff),
  118. PAT(0xff000000),
  119. PAT(0xff0000ff),
  120. PAT(0xff00ff00),
  121. PAT(0xff00ffff),
  122. PAT(0xffff0000),
  123. PAT(0xffff00ff),
  124. PAT(0xffffff00),
  125. PAT(0xffffffff),
  126. };
  127. static const uint32_t dmask4[4] = {
  128. PAT(0x00000000),
  129. PAT(0x0000ffff),
  130. PAT(0xffff0000),
  131. PAT(0xffffffff),
  132. };
  133. static uint32_t expand4[256];
  134. static uint16_t expand2[256];
  135. static uint8_t expand4to8[16];
  136. static void vga_screen_dump(void *opaque, const char *filename);
  137. static const char *screen_dump_filename;
  138. static DisplayChangeListener *screen_dump_dcl;
  139. static void vga_update_memory_access(VGACommonState *s)
  140. {
  141. MemoryRegion *region, *old_region = s->chain4_alias;
  142. target_phys_addr_t base, offset, size;
  143. s->chain4_alias = NULL;
  144. if ((s->sr[0x02] & 0xf) == 0xf && s->sr[0x04] & 0x08) {
  145. offset = 0;
  146. switch ((s->gr[6] >> 2) & 3) {
  147. case 0:
  148. base = 0xa0000;
  149. size = 0x20000;
  150. break;
  151. case 1:
  152. base = 0xa0000;
  153. size = 0x10000;
  154. offset = s->bank_offset;
  155. break;
  156. case 2:
  157. base = 0xb0000;
  158. size = 0x8000;
  159. break;
  160. case 3:
  161. default:
  162. base = 0xb8000;
  163. size = 0x8000;
  164. break;
  165. }
  166. base += isa_mem_base;
  167. region = g_malloc(sizeof(*region));
  168. memory_region_init_alias(region, "vga.chain4", &s->vram, offset, size);
  169. memory_region_add_subregion_overlap(s->legacy_address_space, base,
  170. region, 2);
  171. s->chain4_alias = region;
  172. }
  173. if (old_region) {
  174. memory_region_del_subregion(s->legacy_address_space, old_region);
  175. memory_region_destroy(old_region);
  176. g_free(old_region);
  177. s->plane_updated = 0xf;
  178. }
  179. }
  180. static void vga_dumb_update_retrace_info(VGACommonState *s)
  181. {
  182. (void) s;
  183. }
  184. static void vga_precise_update_retrace_info(VGACommonState *s)
  185. {
  186. int htotal_chars;
  187. int hretr_start_char;
  188. int hretr_skew_chars;
  189. int hretr_end_char;
  190. int vtotal_lines;
  191. int vretr_start_line;
  192. int vretr_end_line;
  193. int dots;
  194. #if 0
  195. int div2, sldiv2;
  196. #endif
  197. int clocking_mode;
  198. int clock_sel;
  199. const int clk_hz[] = {25175000, 28322000, 25175000, 25175000};
  200. int64_t chars_per_sec;
  201. struct vga_precise_retrace *r = &s->retrace_info.precise;
  202. htotal_chars = s->cr[0x00] + 5;
  203. hretr_start_char = s->cr[0x04];
  204. hretr_skew_chars = (s->cr[0x05] >> 5) & 3;
  205. hretr_end_char = s->cr[0x05] & 0x1f;
  206. vtotal_lines = (s->cr[0x06]
  207. | (((s->cr[0x07] & 1) | ((s->cr[0x07] >> 4) & 2)) << 8)) + 2
  208. ;
  209. vretr_start_line = s->cr[0x10]
  210. | ((((s->cr[0x07] >> 2) & 1) | ((s->cr[0x07] >> 6) & 2)) << 8)
  211. ;
  212. vretr_end_line = s->cr[0x11] & 0xf;
  213. clocking_mode = (s->sr[0x01] >> 3) & 1;
  214. clock_sel = (s->msr >> 2) & 3;
  215. dots = (s->msr & 1) ? 8 : 9;
  216. chars_per_sec = clk_hz[clock_sel] / dots;
  217. htotal_chars <<= clocking_mode;
  218. r->total_chars = vtotal_lines * htotal_chars;
  219. if (r->freq) {
  220. r->ticks_per_char = get_ticks_per_sec() / (r->total_chars * r->freq);
  221. } else {
  222. r->ticks_per_char = get_ticks_per_sec() / chars_per_sec;
  223. }
  224. r->vstart = vretr_start_line;
  225. r->vend = r->vstart + vretr_end_line + 1;
  226. r->hstart = hretr_start_char + hretr_skew_chars;
  227. r->hend = r->hstart + hretr_end_char + 1;
  228. r->htotal = htotal_chars;
  229. #if 0
  230. div2 = (s->cr[0x17] >> 2) & 1;
  231. sldiv2 = (s->cr[0x17] >> 3) & 1;
  232. printf (
  233. "hz=%f\n"
  234. "htotal = %d\n"
  235. "hretr_start = %d\n"
  236. "hretr_skew = %d\n"
  237. "hretr_end = %d\n"
  238. "vtotal = %d\n"
  239. "vretr_start = %d\n"
  240. "vretr_end = %d\n"
  241. "div2 = %d sldiv2 = %d\n"
  242. "clocking_mode = %d\n"
  243. "clock_sel = %d %d\n"
  244. "dots = %d\n"
  245. "ticks/char = %" PRId64 "\n"
  246. "\n",
  247. (double) get_ticks_per_sec() / (r->ticks_per_char * r->total_chars),
  248. htotal_chars,
  249. hretr_start_char,
  250. hretr_skew_chars,
  251. hretr_end_char,
  252. vtotal_lines,
  253. vretr_start_line,
  254. vretr_end_line,
  255. div2, sldiv2,
  256. clocking_mode,
  257. clock_sel,
  258. clk_hz[clock_sel],
  259. dots,
  260. r->ticks_per_char
  261. );
  262. #endif
  263. }
  264. static uint8_t vga_precise_retrace(VGACommonState *s)
  265. {
  266. struct vga_precise_retrace *r = &s->retrace_info.precise;
  267. uint8_t val = s->st01 & ~(ST01_V_RETRACE | ST01_DISP_ENABLE);
  268. if (r->total_chars) {
  269. int cur_line, cur_line_char, cur_char;
  270. int64_t cur_tick;
  271. cur_tick = qemu_get_clock_ns(vm_clock);
  272. cur_char = (cur_tick / r->ticks_per_char) % r->total_chars;
  273. cur_line = cur_char / r->htotal;
  274. if (cur_line >= r->vstart && cur_line <= r->vend) {
  275. val |= ST01_V_RETRACE | ST01_DISP_ENABLE;
  276. } else {
  277. cur_line_char = cur_char % r->htotal;
  278. if (cur_line_char >= r->hstart && cur_line_char <= r->hend) {
  279. val |= ST01_DISP_ENABLE;
  280. }
  281. }
  282. return val;
  283. } else {
  284. return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
  285. }
  286. }
  287. static uint8_t vga_dumb_retrace(VGACommonState *s)
  288. {
  289. return s->st01 ^ (ST01_V_RETRACE | ST01_DISP_ENABLE);
  290. }
  291. int vga_ioport_invalid(VGACommonState *s, uint32_t addr)
  292. {
  293. if (s->msr & MSR_COLOR_EMULATION) {
  294. /* Color */
  295. return (addr >= 0x3b0 && addr <= 0x3bf);
  296. } else {
  297. /* Monochrome */
  298. return (addr >= 0x3d0 && addr <= 0x3df);
  299. }
  300. }
  301. uint32_t vga_ioport_read(void *opaque, uint32_t addr)
  302. {
  303. VGACommonState *s = opaque;
  304. int val, index;
  305. if (vga_ioport_invalid(s, addr)) {
  306. val = 0xff;
  307. } else {
  308. switch(addr) {
  309. case 0x3c0:
  310. if (s->ar_flip_flop == 0) {
  311. val = s->ar_index;
  312. } else {
  313. val = 0;
  314. }
  315. break;
  316. case 0x3c1:
  317. index = s->ar_index & 0x1f;
  318. if (index < 21)
  319. val = s->ar[index];
  320. else
  321. val = 0;
  322. break;
  323. case 0x3c2:
  324. val = s->st00;
  325. break;
  326. case 0x3c4:
  327. val = s->sr_index;
  328. break;
  329. case 0x3c5:
  330. val = s->sr[s->sr_index];
  331. #ifdef DEBUG_VGA_REG
  332. printf("vga: read SR%x = 0x%02x\n", s->sr_index, val);
  333. #endif
  334. break;
  335. case 0x3c7:
  336. val = s->dac_state;
  337. break;
  338. case 0x3c8:
  339. val = s->dac_write_index;
  340. break;
  341. case 0x3c9:
  342. val = s->palette[s->dac_read_index * 3 + s->dac_sub_index];
  343. if (++s->dac_sub_index == 3) {
  344. s->dac_sub_index = 0;
  345. s->dac_read_index++;
  346. }
  347. break;
  348. case 0x3ca:
  349. val = s->fcr;
  350. break;
  351. case 0x3cc:
  352. val = s->msr;
  353. break;
  354. case 0x3ce:
  355. val = s->gr_index;
  356. break;
  357. case 0x3cf:
  358. val = s->gr[s->gr_index];
  359. #ifdef DEBUG_VGA_REG
  360. printf("vga: read GR%x = 0x%02x\n", s->gr_index, val);
  361. #endif
  362. break;
  363. case 0x3b4:
  364. case 0x3d4:
  365. val = s->cr_index;
  366. break;
  367. case 0x3b5:
  368. case 0x3d5:
  369. val = s->cr[s->cr_index];
  370. #ifdef DEBUG_VGA_REG
  371. printf("vga: read CR%x = 0x%02x\n", s->cr_index, val);
  372. #endif
  373. break;
  374. case 0x3ba:
  375. case 0x3da:
  376. /* just toggle to fool polling */
  377. val = s->st01 = s->retrace(s);
  378. s->ar_flip_flop = 0;
  379. break;
  380. default:
  381. val = 0x00;
  382. break;
  383. }
  384. }
  385. #if defined(DEBUG_VGA)
  386. printf("VGA: read addr=0x%04x data=0x%02x\n", addr, val);
  387. #endif
  388. return val;
  389. }
  390. void vga_ioport_write(void *opaque, uint32_t addr, uint32_t val)
  391. {
  392. VGACommonState *s = opaque;
  393. int index;
  394. /* check port range access depending on color/monochrome mode */
  395. if (vga_ioport_invalid(s, addr)) {
  396. return;
  397. }
  398. #ifdef DEBUG_VGA
  399. printf("VGA: write addr=0x%04x data=0x%02x\n", addr, val);
  400. #endif
  401. switch(addr) {
  402. case 0x3c0:
  403. if (s->ar_flip_flop == 0) {
  404. val &= 0x3f;
  405. s->ar_index = val;
  406. } else {
  407. index = s->ar_index & 0x1f;
  408. switch(index) {
  409. case 0x00 ... 0x0f:
  410. s->ar[index] = val & 0x3f;
  411. break;
  412. case 0x10:
  413. s->ar[index] = val & ~0x10;
  414. break;
  415. case 0x11:
  416. s->ar[index] = val;
  417. break;
  418. case 0x12:
  419. s->ar[index] = val & ~0xc0;
  420. break;
  421. case 0x13:
  422. s->ar[index] = val & ~0xf0;
  423. break;
  424. case 0x14:
  425. s->ar[index] = val & ~0xf0;
  426. break;
  427. default:
  428. break;
  429. }
  430. }
  431. s->ar_flip_flop ^= 1;
  432. break;
  433. case 0x3c2:
  434. s->msr = val & ~0x10;
  435. s->update_retrace_info(s);
  436. break;
  437. case 0x3c4:
  438. s->sr_index = val & 7;
  439. break;
  440. case 0x3c5:
  441. #ifdef DEBUG_VGA_REG
  442. printf("vga: write SR%x = 0x%02x\n", s->sr_index, val);
  443. #endif
  444. s->sr[s->sr_index] = val & sr_mask[s->sr_index];
  445. if (s->sr_index == 1) s->update_retrace_info(s);
  446. vga_update_memory_access(s);
  447. break;
  448. case 0x3c7:
  449. s->dac_read_index = val;
  450. s->dac_sub_index = 0;
  451. s->dac_state = 3;
  452. break;
  453. case 0x3c8:
  454. s->dac_write_index = val;
  455. s->dac_sub_index = 0;
  456. s->dac_state = 0;
  457. break;
  458. case 0x3c9:
  459. s->dac_cache[s->dac_sub_index] = val;
  460. if (++s->dac_sub_index == 3) {
  461. memcpy(&s->palette[s->dac_write_index * 3], s->dac_cache, 3);
  462. s->dac_sub_index = 0;
  463. s->dac_write_index++;
  464. }
  465. break;
  466. case 0x3ce:
  467. s->gr_index = val & 0x0f;
  468. break;
  469. case 0x3cf:
  470. #ifdef DEBUG_VGA_REG
  471. printf("vga: write GR%x = 0x%02x\n", s->gr_index, val);
  472. #endif
  473. s->gr[s->gr_index] = val & gr_mask[s->gr_index];
  474. vga_update_memory_access(s);
  475. break;
  476. case 0x3b4:
  477. case 0x3d4:
  478. s->cr_index = val;
  479. break;
  480. case 0x3b5:
  481. case 0x3d5:
  482. #ifdef DEBUG_VGA_REG
  483. printf("vga: write CR%x = 0x%02x\n", s->cr_index, val);
  484. #endif
  485. /* handle CR0-7 protection */
  486. if ((s->cr[0x11] & 0x80) && s->cr_index <= 7) {
  487. /* can always write bit 4 of CR7 */
  488. if (s->cr_index == 7)
  489. s->cr[7] = (s->cr[7] & ~0x10) | (val & 0x10);
  490. return;
  491. }
  492. s->cr[s->cr_index] = val;
  493. switch(s->cr_index) {
  494. case 0x00:
  495. case 0x04:
  496. case 0x05:
  497. case 0x06:
  498. case 0x07:
  499. case 0x11:
  500. case 0x17:
  501. s->update_retrace_info(s);
  502. break;
  503. }
  504. break;
  505. case 0x3ba:
  506. case 0x3da:
  507. s->fcr = val & 0x10;
  508. break;
  509. }
  510. }
  511. #ifdef CONFIG_BOCHS_VBE
  512. static uint32_t vbe_ioport_read_index(void *opaque, uint32_t addr)
  513. {
  514. VGACommonState *s = opaque;
  515. uint32_t val;
  516. val = s->vbe_index;
  517. return val;
  518. }
  519. static uint32_t vbe_ioport_read_data(void *opaque, uint32_t addr)
  520. {
  521. VGACommonState *s = opaque;
  522. uint32_t val;
  523. if (s->vbe_index < VBE_DISPI_INDEX_NB) {
  524. if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_GETCAPS) {
  525. switch(s->vbe_index) {
  526. /* XXX: do not hardcode ? */
  527. case VBE_DISPI_INDEX_XRES:
  528. val = VBE_DISPI_MAX_XRES;
  529. break;
  530. case VBE_DISPI_INDEX_YRES:
  531. val = VBE_DISPI_MAX_YRES;
  532. break;
  533. case VBE_DISPI_INDEX_BPP:
  534. val = VBE_DISPI_MAX_BPP;
  535. break;
  536. default:
  537. val = s->vbe_regs[s->vbe_index];
  538. break;
  539. }
  540. } else {
  541. val = s->vbe_regs[s->vbe_index];
  542. }
  543. } else if (s->vbe_index == VBE_DISPI_INDEX_VIDEO_MEMORY_64K) {
  544. val = s->vram_size / (64 * 1024);
  545. } else {
  546. val = 0;
  547. }
  548. #ifdef DEBUG_BOCHS_VBE
  549. printf("VBE: read index=0x%x val=0x%x\n", s->vbe_index, val);
  550. #endif
  551. return val;
  552. }
  553. static void vbe_ioport_write_index(void *opaque, uint32_t addr, uint32_t val)
  554. {
  555. VGACommonState *s = opaque;
  556. s->vbe_index = val;
  557. }
  558. static void vbe_ioport_write_data(void *opaque, uint32_t addr, uint32_t val)
  559. {
  560. VGACommonState *s = opaque;
  561. if (s->vbe_index <= VBE_DISPI_INDEX_NB) {
  562. #ifdef DEBUG_BOCHS_VBE
  563. printf("VBE: write index=0x%x val=0x%x\n", s->vbe_index, val);
  564. #endif
  565. switch(s->vbe_index) {
  566. case VBE_DISPI_INDEX_ID:
  567. if (val == VBE_DISPI_ID0 ||
  568. val == VBE_DISPI_ID1 ||
  569. val == VBE_DISPI_ID2 ||
  570. val == VBE_DISPI_ID3 ||
  571. val == VBE_DISPI_ID4) {
  572. s->vbe_regs[s->vbe_index] = val;
  573. }
  574. break;
  575. case VBE_DISPI_INDEX_XRES:
  576. if ((val <= VBE_DISPI_MAX_XRES) && ((val & 7) == 0)) {
  577. s->vbe_regs[s->vbe_index] = val;
  578. }
  579. break;
  580. case VBE_DISPI_INDEX_YRES:
  581. if (val <= VBE_DISPI_MAX_YRES) {
  582. s->vbe_regs[s->vbe_index] = val;
  583. }
  584. break;
  585. case VBE_DISPI_INDEX_BPP:
  586. if (val == 0)
  587. val = 8;
  588. if (val == 4 || val == 8 || val == 15 ||
  589. val == 16 || val == 24 || val == 32) {
  590. s->vbe_regs[s->vbe_index] = val;
  591. }
  592. break;
  593. case VBE_DISPI_INDEX_BANK:
  594. if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
  595. val &= (s->vbe_bank_mask >> 2);
  596. } else {
  597. val &= s->vbe_bank_mask;
  598. }
  599. s->vbe_regs[s->vbe_index] = val;
  600. s->bank_offset = (val << 16);
  601. vga_update_memory_access(s);
  602. break;
  603. case VBE_DISPI_INDEX_ENABLE:
  604. if ((val & VBE_DISPI_ENABLED) &&
  605. !(s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED)) {
  606. int h, shift_control;
  607. s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] =
  608. s->vbe_regs[VBE_DISPI_INDEX_XRES];
  609. s->vbe_regs[VBE_DISPI_INDEX_VIRT_HEIGHT] =
  610. s->vbe_regs[VBE_DISPI_INDEX_YRES];
  611. s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET] = 0;
  612. s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET] = 0;
  613. if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
  614. s->vbe_line_offset = s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 1;
  615. else
  616. s->vbe_line_offset = s->vbe_regs[VBE_DISPI_INDEX_XRES] *
  617. ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
  618. s->vbe_start_addr = 0;
  619. /* clear the screen (should be done in BIOS) */
  620. if (!(val & VBE_DISPI_NOCLEARMEM)) {
  621. memset(s->vram_ptr, 0,
  622. s->vbe_regs[VBE_DISPI_INDEX_YRES] * s->vbe_line_offset);
  623. }
  624. /* we initialize the VGA graphic mode (should be done
  625. in BIOS) */
  626. s->gr[0x06] = (s->gr[0x06] & ~0x0c) | 0x05; /* graphic mode + memory map 1 */
  627. s->cr[0x17] |= 3; /* no CGA modes */
  628. s->cr[0x13] = s->vbe_line_offset >> 3;
  629. /* width */
  630. s->cr[0x01] = (s->vbe_regs[VBE_DISPI_INDEX_XRES] >> 3) - 1;
  631. /* height (only meaningful if < 1024) */
  632. h = s->vbe_regs[VBE_DISPI_INDEX_YRES] - 1;
  633. s->cr[0x12] = h;
  634. s->cr[0x07] = (s->cr[0x07] & ~0x42) |
  635. ((h >> 7) & 0x02) | ((h >> 3) & 0x40);
  636. /* line compare to 1023 */
  637. s->cr[0x18] = 0xff;
  638. s->cr[0x07] |= 0x10;
  639. s->cr[0x09] |= 0x40;
  640. if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4) {
  641. shift_control = 0;
  642. s->sr[0x01] &= ~8; /* no double line */
  643. } else {
  644. shift_control = 2;
  645. s->sr[4] |= 0x08; /* set chain 4 mode */
  646. s->sr[2] |= 0x0f; /* activate all planes */
  647. }
  648. s->gr[0x05] = (s->gr[0x05] & ~0x60) | (shift_control << 5);
  649. s->cr[0x09] &= ~0x9f; /* no double scan */
  650. } else {
  651. /* XXX: the bios should do that */
  652. s->bank_offset = 0;
  653. }
  654. s->dac_8bit = (val & VBE_DISPI_8BIT_DAC) > 0;
  655. s->vbe_regs[s->vbe_index] = val;
  656. vga_update_memory_access(s);
  657. break;
  658. case VBE_DISPI_INDEX_VIRT_WIDTH:
  659. {
  660. int w, h, line_offset;
  661. if (val < s->vbe_regs[VBE_DISPI_INDEX_XRES])
  662. return;
  663. w = val;
  664. if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
  665. line_offset = w >> 1;
  666. else
  667. line_offset = w * ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
  668. h = s->vram_size / line_offset;
  669. /* XXX: support weird bochs semantics ? */
  670. if (h < s->vbe_regs[VBE_DISPI_INDEX_YRES])
  671. return;
  672. s->vbe_regs[VBE_DISPI_INDEX_VIRT_WIDTH] = w;
  673. s->vbe_regs[VBE_DISPI_INDEX_VIRT_HEIGHT] = h;
  674. s->vbe_line_offset = line_offset;
  675. }
  676. break;
  677. case VBE_DISPI_INDEX_X_OFFSET:
  678. case VBE_DISPI_INDEX_Y_OFFSET:
  679. {
  680. int x;
  681. s->vbe_regs[s->vbe_index] = val;
  682. s->vbe_start_addr = s->vbe_line_offset * s->vbe_regs[VBE_DISPI_INDEX_Y_OFFSET];
  683. x = s->vbe_regs[VBE_DISPI_INDEX_X_OFFSET];
  684. if (s->vbe_regs[VBE_DISPI_INDEX_BPP] == 4)
  685. s->vbe_start_addr += x >> 1;
  686. else
  687. s->vbe_start_addr += x * ((s->vbe_regs[VBE_DISPI_INDEX_BPP] + 7) >> 3);
  688. s->vbe_start_addr >>= 2;
  689. }
  690. break;
  691. default:
  692. break;
  693. }
  694. }
  695. }
  696. #endif
  697. /* called for accesses between 0xa0000 and 0xc0000 */
  698. uint32_t vga_mem_readb(VGACommonState *s, target_phys_addr_t addr)
  699. {
  700. int memory_map_mode, plane;
  701. uint32_t ret;
  702. /* convert to VGA memory offset */
  703. memory_map_mode = (s->gr[6] >> 2) & 3;
  704. addr &= 0x1ffff;
  705. switch(memory_map_mode) {
  706. case 0:
  707. break;
  708. case 1:
  709. if (addr >= 0x10000)
  710. return 0xff;
  711. addr += s->bank_offset;
  712. break;
  713. case 2:
  714. addr -= 0x10000;
  715. if (addr >= 0x8000)
  716. return 0xff;
  717. break;
  718. default:
  719. case 3:
  720. addr -= 0x18000;
  721. if (addr >= 0x8000)
  722. return 0xff;
  723. break;
  724. }
  725. if (s->sr[4] & 0x08) {
  726. /* chain 4 mode : simplest access */
  727. ret = s->vram_ptr[addr];
  728. } else if (s->gr[5] & 0x10) {
  729. /* odd/even mode (aka text mode mapping) */
  730. plane = (s->gr[4] & 2) | (addr & 1);
  731. ret = s->vram_ptr[((addr & ~1) << 1) | plane];
  732. } else {
  733. /* standard VGA latched access */
  734. s->latch = ((uint32_t *)s->vram_ptr)[addr];
  735. if (!(s->gr[5] & 0x08)) {
  736. /* read mode 0 */
  737. plane = s->gr[4];
  738. ret = GET_PLANE(s->latch, plane);
  739. } else {
  740. /* read mode 1 */
  741. ret = (s->latch ^ mask16[s->gr[2]]) & mask16[s->gr[7]];
  742. ret |= ret >> 16;
  743. ret |= ret >> 8;
  744. ret = (~ret) & 0xff;
  745. }
  746. }
  747. return ret;
  748. }
  749. /* called for accesses between 0xa0000 and 0xc0000 */
  750. void vga_mem_writeb(VGACommonState *s, target_phys_addr_t addr, uint32_t val)
  751. {
  752. int memory_map_mode, plane, write_mode, b, func_select, mask;
  753. uint32_t write_mask, bit_mask, set_mask;
  754. #ifdef DEBUG_VGA_MEM
  755. printf("vga: [0x" TARGET_FMT_plx "] = 0x%02x\n", addr, val);
  756. #endif
  757. /* convert to VGA memory offset */
  758. memory_map_mode = (s->gr[6] >> 2) & 3;
  759. addr &= 0x1ffff;
  760. switch(memory_map_mode) {
  761. case 0:
  762. break;
  763. case 1:
  764. if (addr >= 0x10000)
  765. return;
  766. addr += s->bank_offset;
  767. break;
  768. case 2:
  769. addr -= 0x10000;
  770. if (addr >= 0x8000)
  771. return;
  772. break;
  773. default:
  774. case 3:
  775. addr -= 0x18000;
  776. if (addr >= 0x8000)
  777. return;
  778. break;
  779. }
  780. if (s->sr[4] & 0x08) {
  781. /* chain 4 mode : simplest access */
  782. plane = addr & 3;
  783. mask = (1 << plane);
  784. if (s->sr[2] & mask) {
  785. s->vram_ptr[addr] = val;
  786. #ifdef DEBUG_VGA_MEM
  787. printf("vga: chain4: [0x" TARGET_FMT_plx "]\n", addr);
  788. #endif
  789. s->plane_updated |= mask; /* only used to detect font change */
  790. memory_region_set_dirty(&s->vram, addr);
  791. }
  792. } else if (s->gr[5] & 0x10) {
  793. /* odd/even mode (aka text mode mapping) */
  794. plane = (s->gr[4] & 2) | (addr & 1);
  795. mask = (1 << plane);
  796. if (s->sr[2] & mask) {
  797. addr = ((addr & ~1) << 1) | plane;
  798. s->vram_ptr[addr] = val;
  799. #ifdef DEBUG_VGA_MEM
  800. printf("vga: odd/even: [0x" TARGET_FMT_plx "]\n", addr);
  801. #endif
  802. s->plane_updated |= mask; /* only used to detect font change */
  803. memory_region_set_dirty(&s->vram, addr);
  804. }
  805. } else {
  806. /* standard VGA latched access */
  807. write_mode = s->gr[5] & 3;
  808. switch(write_mode) {
  809. default:
  810. case 0:
  811. /* rotate */
  812. b = s->gr[3] & 7;
  813. val = ((val >> b) | (val << (8 - b))) & 0xff;
  814. val |= val << 8;
  815. val |= val << 16;
  816. /* apply set/reset mask */
  817. set_mask = mask16[s->gr[1]];
  818. val = (val & ~set_mask) | (mask16[s->gr[0]] & set_mask);
  819. bit_mask = s->gr[8];
  820. break;
  821. case 1:
  822. val = s->latch;
  823. goto do_write;
  824. case 2:
  825. val = mask16[val & 0x0f];
  826. bit_mask = s->gr[8];
  827. break;
  828. case 3:
  829. /* rotate */
  830. b = s->gr[3] & 7;
  831. val = (val >> b) | (val << (8 - b));
  832. bit_mask = s->gr[8] & val;
  833. val = mask16[s->gr[0]];
  834. break;
  835. }
  836. /* apply logical operation */
  837. func_select = s->gr[3] >> 3;
  838. switch(func_select) {
  839. case 0:
  840. default:
  841. /* nothing to do */
  842. break;
  843. case 1:
  844. /* and */
  845. val &= s->latch;
  846. break;
  847. case 2:
  848. /* or */
  849. val |= s->latch;
  850. break;
  851. case 3:
  852. /* xor */
  853. val ^= s->latch;
  854. break;
  855. }
  856. /* apply bit mask */
  857. bit_mask |= bit_mask << 8;
  858. bit_mask |= bit_mask << 16;
  859. val = (val & bit_mask) | (s->latch & ~bit_mask);
  860. do_write:
  861. /* mask data according to sr[2] */
  862. mask = s->sr[2];
  863. s->plane_updated |= mask; /* only used to detect font change */
  864. write_mask = mask16[mask];
  865. ((uint32_t *)s->vram_ptr)[addr] =
  866. (((uint32_t *)s->vram_ptr)[addr] & ~write_mask) |
  867. (val & write_mask);
  868. #ifdef DEBUG_VGA_MEM
  869. printf("vga: latch: [0x" TARGET_FMT_plx "] mask=0x%08x val=0x%08x\n",
  870. addr * 4, write_mask, val);
  871. #endif
  872. memory_region_set_dirty(&s->vram, addr << 2);
  873. }
  874. }
  875. typedef void vga_draw_glyph8_func(uint8_t *d, int linesize,
  876. const uint8_t *font_ptr, int h,
  877. uint32_t fgcol, uint32_t bgcol);
  878. typedef void vga_draw_glyph9_func(uint8_t *d, int linesize,
  879. const uint8_t *font_ptr, int h,
  880. uint32_t fgcol, uint32_t bgcol, int dup9);
  881. typedef void vga_draw_line_func(VGACommonState *s1, uint8_t *d,
  882. const uint8_t *s, int width);
  883. #define DEPTH 8
  884. #include "vga_template.h"
  885. #define DEPTH 15
  886. #include "vga_template.h"
  887. #define BGR_FORMAT
  888. #define DEPTH 15
  889. #include "vga_template.h"
  890. #define DEPTH 16
  891. #include "vga_template.h"
  892. #define BGR_FORMAT
  893. #define DEPTH 16
  894. #include "vga_template.h"
  895. #define DEPTH 32
  896. #include "vga_template.h"
  897. #define BGR_FORMAT
  898. #define DEPTH 32
  899. #include "vga_template.h"
  900. static unsigned int rgb_to_pixel8_dup(unsigned int r, unsigned int g, unsigned b)
  901. {
  902. unsigned int col;
  903. col = rgb_to_pixel8(r, g, b);
  904. col |= col << 8;
  905. col |= col << 16;
  906. return col;
  907. }
  908. static unsigned int rgb_to_pixel15_dup(unsigned int r, unsigned int g, unsigned b)
  909. {
  910. unsigned int col;
  911. col = rgb_to_pixel15(r, g, b);
  912. col |= col << 16;
  913. return col;
  914. }
  915. static unsigned int rgb_to_pixel15bgr_dup(unsigned int r, unsigned int g,
  916. unsigned int b)
  917. {
  918. unsigned int col;
  919. col = rgb_to_pixel15bgr(r, g, b);
  920. col |= col << 16;
  921. return col;
  922. }
  923. static unsigned int rgb_to_pixel16_dup(unsigned int r, unsigned int g, unsigned b)
  924. {
  925. unsigned int col;
  926. col = rgb_to_pixel16(r, g, b);
  927. col |= col << 16;
  928. return col;
  929. }
  930. static unsigned int rgb_to_pixel16bgr_dup(unsigned int r, unsigned int g,
  931. unsigned int b)
  932. {
  933. unsigned int col;
  934. col = rgb_to_pixel16bgr(r, g, b);
  935. col |= col << 16;
  936. return col;
  937. }
  938. static unsigned int rgb_to_pixel32_dup(unsigned int r, unsigned int g, unsigned b)
  939. {
  940. unsigned int col;
  941. col = rgb_to_pixel32(r, g, b);
  942. return col;
  943. }
  944. static unsigned int rgb_to_pixel32bgr_dup(unsigned int r, unsigned int g, unsigned b)
  945. {
  946. unsigned int col;
  947. col = rgb_to_pixel32bgr(r, g, b);
  948. return col;
  949. }
  950. /* return true if the palette was modified */
  951. static int update_palette16(VGACommonState *s)
  952. {
  953. int full_update, i;
  954. uint32_t v, col, *palette;
  955. full_update = 0;
  956. palette = s->last_palette;
  957. for(i = 0; i < 16; i++) {
  958. v = s->ar[i];
  959. if (s->ar[0x10] & 0x80)
  960. v = ((s->ar[0x14] & 0xf) << 4) | (v & 0xf);
  961. else
  962. v = ((s->ar[0x14] & 0xc) << 4) | (v & 0x3f);
  963. v = v * 3;
  964. col = s->rgb_to_pixel(c6_to_8(s->palette[v]),
  965. c6_to_8(s->palette[v + 1]),
  966. c6_to_8(s->palette[v + 2]));
  967. if (col != palette[i]) {
  968. full_update = 1;
  969. palette[i] = col;
  970. }
  971. }
  972. return full_update;
  973. }
  974. /* return true if the palette was modified */
  975. static int update_palette256(VGACommonState *s)
  976. {
  977. int full_update, i;
  978. uint32_t v, col, *palette;
  979. full_update = 0;
  980. palette = s->last_palette;
  981. v = 0;
  982. for(i = 0; i < 256; i++) {
  983. if (s->dac_8bit) {
  984. col = s->rgb_to_pixel(s->palette[v],
  985. s->palette[v + 1],
  986. s->palette[v + 2]);
  987. } else {
  988. col = s->rgb_to_pixel(c6_to_8(s->palette[v]),
  989. c6_to_8(s->palette[v + 1]),
  990. c6_to_8(s->palette[v + 2]));
  991. }
  992. if (col != palette[i]) {
  993. full_update = 1;
  994. palette[i] = col;
  995. }
  996. v += 3;
  997. }
  998. return full_update;
  999. }
  1000. static void vga_get_offsets(VGACommonState *s,
  1001. uint32_t *pline_offset,
  1002. uint32_t *pstart_addr,
  1003. uint32_t *pline_compare)
  1004. {
  1005. uint32_t start_addr, line_offset, line_compare;
  1006. #ifdef CONFIG_BOCHS_VBE
  1007. if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
  1008. line_offset = s->vbe_line_offset;
  1009. start_addr = s->vbe_start_addr;
  1010. line_compare = 65535;
  1011. } else
  1012. #endif
  1013. {
  1014. /* compute line_offset in bytes */
  1015. line_offset = s->cr[0x13];
  1016. line_offset <<= 3;
  1017. /* starting address */
  1018. start_addr = s->cr[0x0d] | (s->cr[0x0c] << 8);
  1019. /* line compare */
  1020. line_compare = s->cr[0x18] |
  1021. ((s->cr[0x07] & 0x10) << 4) |
  1022. ((s->cr[0x09] & 0x40) << 3);
  1023. }
  1024. *pline_offset = line_offset;
  1025. *pstart_addr = start_addr;
  1026. *pline_compare = line_compare;
  1027. }
  1028. /* update start_addr and line_offset. Return TRUE if modified */
  1029. static int update_basic_params(VGACommonState *s)
  1030. {
  1031. int full_update;
  1032. uint32_t start_addr, line_offset, line_compare;
  1033. full_update = 0;
  1034. s->get_offsets(s, &line_offset, &start_addr, &line_compare);
  1035. if (line_offset != s->line_offset ||
  1036. start_addr != s->start_addr ||
  1037. line_compare != s->line_compare) {
  1038. s->line_offset = line_offset;
  1039. s->start_addr = start_addr;
  1040. s->line_compare = line_compare;
  1041. full_update = 1;
  1042. }
  1043. return full_update;
  1044. }
  1045. #define NB_DEPTHS 7
  1046. static inline int get_depth_index(DisplayState *s)
  1047. {
  1048. switch(ds_get_bits_per_pixel(s)) {
  1049. default:
  1050. case 8:
  1051. return 0;
  1052. case 15:
  1053. return 1;
  1054. case 16:
  1055. return 2;
  1056. case 32:
  1057. if (is_surface_bgr(s->surface))
  1058. return 4;
  1059. else
  1060. return 3;
  1061. }
  1062. }
  1063. static vga_draw_glyph8_func * const vga_draw_glyph8_table[NB_DEPTHS] = {
  1064. vga_draw_glyph8_8,
  1065. vga_draw_glyph8_16,
  1066. vga_draw_glyph8_16,
  1067. vga_draw_glyph8_32,
  1068. vga_draw_glyph8_32,
  1069. vga_draw_glyph8_16,
  1070. vga_draw_glyph8_16,
  1071. };
  1072. static vga_draw_glyph8_func * const vga_draw_glyph16_table[NB_DEPTHS] = {
  1073. vga_draw_glyph16_8,
  1074. vga_draw_glyph16_16,
  1075. vga_draw_glyph16_16,
  1076. vga_draw_glyph16_32,
  1077. vga_draw_glyph16_32,
  1078. vga_draw_glyph16_16,
  1079. vga_draw_glyph16_16,
  1080. };
  1081. static vga_draw_glyph9_func * const vga_draw_glyph9_table[NB_DEPTHS] = {
  1082. vga_draw_glyph9_8,
  1083. vga_draw_glyph9_16,
  1084. vga_draw_glyph9_16,
  1085. vga_draw_glyph9_32,
  1086. vga_draw_glyph9_32,
  1087. vga_draw_glyph9_16,
  1088. vga_draw_glyph9_16,
  1089. };
  1090. static const uint8_t cursor_glyph[32 * 4] = {
  1091. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1092. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1093. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1094. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1095. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1096. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1097. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1098. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1099. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1100. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1101. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1102. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1103. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1104. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1105. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1106. 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  1107. };
  1108. static void vga_get_text_resolution(VGACommonState *s, int *pwidth, int *pheight,
  1109. int *pcwidth, int *pcheight)
  1110. {
  1111. int width, cwidth, height, cheight;
  1112. /* total width & height */
  1113. cheight = (s->cr[9] & 0x1f) + 1;
  1114. cwidth = 8;
  1115. if (!(s->sr[1] & 0x01))
  1116. cwidth = 9;
  1117. if (s->sr[1] & 0x08)
  1118. cwidth = 16; /* NOTE: no 18 pixel wide */
  1119. width = (s->cr[0x01] + 1);
  1120. if (s->cr[0x06] == 100) {
  1121. /* ugly hack for CGA 160x100x16 - explain me the logic */
  1122. height = 100;
  1123. } else {
  1124. height = s->cr[0x12] |
  1125. ((s->cr[0x07] & 0x02) << 7) |
  1126. ((s->cr[0x07] & 0x40) << 3);
  1127. height = (height + 1) / cheight;
  1128. }
  1129. *pwidth = width;
  1130. *pheight = height;
  1131. *pcwidth = cwidth;
  1132. *pcheight = cheight;
  1133. }
  1134. typedef unsigned int rgb_to_pixel_dup_func(unsigned int r, unsigned int g, unsigned b);
  1135. static rgb_to_pixel_dup_func * const rgb_to_pixel_dup_table[NB_DEPTHS] = {
  1136. rgb_to_pixel8_dup,
  1137. rgb_to_pixel15_dup,
  1138. rgb_to_pixel16_dup,
  1139. rgb_to_pixel32_dup,
  1140. rgb_to_pixel32bgr_dup,
  1141. rgb_to_pixel15bgr_dup,
  1142. rgb_to_pixel16bgr_dup,
  1143. };
  1144. /*
  1145. * Text mode update
  1146. * Missing:
  1147. * - double scan
  1148. * - double width
  1149. * - underline
  1150. * - flashing
  1151. */
  1152. static void vga_draw_text(VGACommonState *s, int full_update)
  1153. {
  1154. int cx, cy, cheight, cw, ch, cattr, height, width, ch_attr;
  1155. int cx_min, cx_max, linesize, x_incr, line, line1;
  1156. uint32_t offset, fgcol, bgcol, v, cursor_offset;
  1157. uint8_t *d1, *d, *src, *dest, *cursor_ptr;
  1158. const uint8_t *font_ptr, *font_base[2];
  1159. int dup9, line_offset, depth_index;
  1160. uint32_t *palette;
  1161. uint32_t *ch_attr_ptr;
  1162. vga_draw_glyph8_func *vga_draw_glyph8;
  1163. vga_draw_glyph9_func *vga_draw_glyph9;
  1164. /* compute font data address (in plane 2) */
  1165. v = s->sr[3];
  1166. offset = (((v >> 4) & 1) | ((v << 1) & 6)) * 8192 * 4 + 2;
  1167. if (offset != s->font_offsets[0]) {
  1168. s->font_offsets[0] = offset;
  1169. full_update = 1;
  1170. }
  1171. font_base[0] = s->vram_ptr + offset;
  1172. offset = (((v >> 5) & 1) | ((v >> 1) & 6)) * 8192 * 4 + 2;
  1173. font_base[1] = s->vram_ptr + offset;
  1174. if (offset != s->font_offsets[1]) {
  1175. s->font_offsets[1] = offset;
  1176. full_update = 1;
  1177. }
  1178. if (s->plane_updated & (1 << 2) || s->chain4_alias) {
  1179. /* if the plane 2 was modified since the last display, it
  1180. indicates the font may have been modified */
  1181. s->plane_updated = 0;
  1182. full_update = 1;
  1183. }
  1184. full_update |= update_basic_params(s);
  1185. line_offset = s->line_offset;
  1186. vga_get_text_resolution(s, &width, &height, &cw, &cheight);
  1187. if ((height * width) > CH_ATTR_SIZE) {
  1188. /* better than nothing: exit if transient size is too big */
  1189. return;
  1190. }
  1191. if (width != s->last_width || height != s->last_height ||
  1192. cw != s->last_cw || cheight != s->last_ch || s->last_depth) {
  1193. s->last_scr_width = width * cw;
  1194. s->last_scr_height = height * cheight;
  1195. qemu_console_resize(s->ds, s->last_scr_width, s->last_scr_height);
  1196. s->last_depth = 0;
  1197. s->last_width = width;
  1198. s->last_height = height;
  1199. s->last_ch = cheight;
  1200. s->last_cw = cw;
  1201. full_update = 1;
  1202. }
  1203. s->rgb_to_pixel =
  1204. rgb_to_pixel_dup_table[get_depth_index(s->ds)];
  1205. full_update |= update_palette16(s);
  1206. palette = s->last_palette;
  1207. x_incr = cw * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
  1208. cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - s->start_addr;
  1209. if (cursor_offset != s->cursor_offset ||
  1210. s->cr[0xa] != s->cursor_start ||
  1211. s->cr[0xb] != s->cursor_end) {
  1212. /* if the cursor position changed, we update the old and new
  1213. chars */
  1214. if (s->cursor_offset < CH_ATTR_SIZE)
  1215. s->last_ch_attr[s->cursor_offset] = -1;
  1216. if (cursor_offset < CH_ATTR_SIZE)
  1217. s->last_ch_attr[cursor_offset] = -1;
  1218. s->cursor_offset = cursor_offset;
  1219. s->cursor_start = s->cr[0xa];
  1220. s->cursor_end = s->cr[0xb];
  1221. }
  1222. cursor_ptr = s->vram_ptr + (s->start_addr + cursor_offset) * 4;
  1223. depth_index = get_depth_index(s->ds);
  1224. if (cw == 16)
  1225. vga_draw_glyph8 = vga_draw_glyph16_table[depth_index];
  1226. else
  1227. vga_draw_glyph8 = vga_draw_glyph8_table[depth_index];
  1228. vga_draw_glyph9 = vga_draw_glyph9_table[depth_index];
  1229. dest = ds_get_data(s->ds);
  1230. linesize = ds_get_linesize(s->ds);
  1231. ch_attr_ptr = s->last_ch_attr;
  1232. line = 0;
  1233. offset = s->start_addr * 4;
  1234. for(cy = 0; cy < height; cy++) {
  1235. d1 = dest;
  1236. src = s->vram_ptr + offset;
  1237. cx_min = width;
  1238. cx_max = -1;
  1239. for(cx = 0; cx < width; cx++) {
  1240. ch_attr = *(uint16_t *)src;
  1241. if (full_update || ch_attr != *ch_attr_ptr) {
  1242. if (cx < cx_min)
  1243. cx_min = cx;
  1244. if (cx > cx_max)
  1245. cx_max = cx;
  1246. *ch_attr_ptr = ch_attr;
  1247. #ifdef HOST_WORDS_BIGENDIAN
  1248. ch = ch_attr >> 8;
  1249. cattr = ch_attr & 0xff;
  1250. #else
  1251. ch = ch_attr & 0xff;
  1252. cattr = ch_attr >> 8;
  1253. #endif
  1254. font_ptr = font_base[(cattr >> 3) & 1];
  1255. font_ptr += 32 * 4 * ch;
  1256. bgcol = palette[cattr >> 4];
  1257. fgcol = palette[cattr & 0x0f];
  1258. if (cw != 9) {
  1259. vga_draw_glyph8(d1, linesize,
  1260. font_ptr, cheight, fgcol, bgcol);
  1261. } else {
  1262. dup9 = 0;
  1263. if (ch >= 0xb0 && ch <= 0xdf && (s->ar[0x10] & 0x04))
  1264. dup9 = 1;
  1265. vga_draw_glyph9(d1, linesize,
  1266. font_ptr, cheight, fgcol, bgcol, dup9);
  1267. }
  1268. if (src == cursor_ptr &&
  1269. !(s->cr[0x0a] & 0x20)) {
  1270. int line_start, line_last, h;
  1271. /* draw the cursor */
  1272. line_start = s->cr[0x0a] & 0x1f;
  1273. line_last = s->cr[0x0b] & 0x1f;
  1274. /* XXX: check that */
  1275. if (line_last > cheight - 1)
  1276. line_last = cheight - 1;
  1277. if (line_last >= line_start && line_start < cheight) {
  1278. h = line_last - line_start + 1;
  1279. d = d1 + linesize * line_start;
  1280. if (cw != 9) {
  1281. vga_draw_glyph8(d, linesize,
  1282. cursor_glyph, h, fgcol, bgcol);
  1283. } else {
  1284. vga_draw_glyph9(d, linesize,
  1285. cursor_glyph, h, fgcol, bgcol, 1);
  1286. }
  1287. }
  1288. }
  1289. }
  1290. d1 += x_incr;
  1291. src += 4;
  1292. ch_attr_ptr++;
  1293. }
  1294. if (cx_max != -1) {
  1295. dpy_update(s->ds, cx_min * cw, cy * cheight,
  1296. (cx_max - cx_min + 1) * cw, cheight);
  1297. }
  1298. dest += linesize * cheight;
  1299. line1 = line + cheight;
  1300. offset += line_offset;
  1301. if (line < s->line_compare && line1 >= s->line_compare) {
  1302. offset = 0;
  1303. }
  1304. line = line1;
  1305. }
  1306. }
  1307. enum {
  1308. VGA_DRAW_LINE2,
  1309. VGA_DRAW_LINE2D2,
  1310. VGA_DRAW_LINE4,
  1311. VGA_DRAW_LINE4D2,
  1312. VGA_DRAW_LINE8D2,
  1313. VGA_DRAW_LINE8,
  1314. VGA_DRAW_LINE15,
  1315. VGA_DRAW_LINE16,
  1316. VGA_DRAW_LINE24,
  1317. VGA_DRAW_LINE32,
  1318. VGA_DRAW_LINE_NB,
  1319. };
  1320. static vga_draw_line_func * const vga_draw_line_table[NB_DEPTHS * VGA_DRAW_LINE_NB] = {
  1321. vga_draw_line2_8,
  1322. vga_draw_line2_16,
  1323. vga_draw_line2_16,
  1324. vga_draw_line2_32,
  1325. vga_draw_line2_32,
  1326. vga_draw_line2_16,
  1327. vga_draw_line2_16,
  1328. vga_draw_line2d2_8,
  1329. vga_draw_line2d2_16,
  1330. vga_draw_line2d2_16,
  1331. vga_draw_line2d2_32,
  1332. vga_draw_line2d2_32,
  1333. vga_draw_line2d2_16,
  1334. vga_draw_line2d2_16,
  1335. vga_draw_line4_8,
  1336. vga_draw_line4_16,
  1337. vga_draw_line4_16,
  1338. vga_draw_line4_32,
  1339. vga_draw_line4_32,
  1340. vga_draw_line4_16,
  1341. vga_draw_line4_16,
  1342. vga_draw_line4d2_8,
  1343. vga_draw_line4d2_16,
  1344. vga_draw_line4d2_16,
  1345. vga_draw_line4d2_32,
  1346. vga_draw_line4d2_32,
  1347. vga_draw_line4d2_16,
  1348. vga_draw_line4d2_16,
  1349. vga_draw_line8d2_8,
  1350. vga_draw_line8d2_16,
  1351. vga_draw_line8d2_16,
  1352. vga_draw_line8d2_32,
  1353. vga_draw_line8d2_32,
  1354. vga_draw_line8d2_16,
  1355. vga_draw_line8d2_16,
  1356. vga_draw_line8_8,
  1357. vga_draw_line8_16,
  1358. vga_draw_line8_16,
  1359. vga_draw_line8_32,
  1360. vga_draw_line8_32,
  1361. vga_draw_line8_16,
  1362. vga_draw_line8_16,
  1363. vga_draw_line15_8,
  1364. vga_draw_line15_15,
  1365. vga_draw_line15_16,
  1366. vga_draw_line15_32,
  1367. vga_draw_line15_32bgr,
  1368. vga_draw_line15_15bgr,
  1369. vga_draw_line15_16bgr,
  1370. vga_draw_line16_8,
  1371. vga_draw_line16_15,
  1372. vga_draw_line16_16,
  1373. vga_draw_line16_32,
  1374. vga_draw_line16_32bgr,
  1375. vga_draw_line16_15bgr,
  1376. vga_draw_line16_16bgr,
  1377. vga_draw_line24_8,
  1378. vga_draw_line24_15,
  1379. vga_draw_line24_16,
  1380. vga_draw_line24_32,
  1381. vga_draw_line24_32bgr,
  1382. vga_draw_line24_15bgr,
  1383. vga_draw_line24_16bgr,
  1384. vga_draw_line32_8,
  1385. vga_draw_line32_15,
  1386. vga_draw_line32_16,
  1387. vga_draw_line32_32,
  1388. vga_draw_line32_32bgr,
  1389. vga_draw_line32_15bgr,
  1390. vga_draw_line32_16bgr,
  1391. };
  1392. static int vga_get_bpp(VGACommonState *s)
  1393. {
  1394. int ret;
  1395. #ifdef CONFIG_BOCHS_VBE
  1396. if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
  1397. ret = s->vbe_regs[VBE_DISPI_INDEX_BPP];
  1398. } else
  1399. #endif
  1400. {
  1401. ret = 0;
  1402. }
  1403. return ret;
  1404. }
  1405. static void vga_get_resolution(VGACommonState *s, int *pwidth, int *pheight)
  1406. {
  1407. int width, height;
  1408. #ifdef CONFIG_BOCHS_VBE
  1409. if (s->vbe_regs[VBE_DISPI_INDEX_ENABLE] & VBE_DISPI_ENABLED) {
  1410. width = s->vbe_regs[VBE_DISPI_INDEX_XRES];
  1411. height = s->vbe_regs[VBE_DISPI_INDEX_YRES];
  1412. } else
  1413. #endif
  1414. {
  1415. width = (s->cr[0x01] + 1) * 8;
  1416. height = s->cr[0x12] |
  1417. ((s->cr[0x07] & 0x02) << 7) |
  1418. ((s->cr[0x07] & 0x40) << 3);
  1419. height = (height + 1);
  1420. }
  1421. *pwidth = width;
  1422. *pheight = height;
  1423. }
  1424. void vga_invalidate_scanlines(VGACommonState *s, int y1, int y2)
  1425. {
  1426. int y;
  1427. if (y1 >= VGA_MAX_HEIGHT)
  1428. return;
  1429. if (y2 >= VGA_MAX_HEIGHT)
  1430. y2 = VGA_MAX_HEIGHT;
  1431. for(y = y1; y < y2; y++) {
  1432. s->invalidated_y_table[y >> 5] |= 1 << (y & 0x1f);
  1433. }
  1434. }
  1435. static void vga_sync_dirty_bitmap(VGACommonState *s)
  1436. {
  1437. memory_region_sync_dirty_bitmap(&s->vram);
  1438. }
  1439. void vga_dirty_log_start(VGACommonState *s)
  1440. {
  1441. memory_region_set_log(&s->vram, true, DIRTY_MEMORY_VGA);
  1442. }
  1443. void vga_dirty_log_stop(VGACommonState *s)
  1444. {
  1445. memory_region_set_log(&s->vram, false, DIRTY_MEMORY_VGA);
  1446. }
  1447. /*
  1448. * graphic modes
  1449. */
  1450. static void vga_draw_graphic(VGACommonState *s, int full_update)
  1451. {
  1452. int y1, y, update, linesize, y_start, double_scan, mask, depth;
  1453. int width, height, shift_control, line_offset, bwidth, bits;
  1454. ram_addr_t page0, page1, page_min, page_max;
  1455. int disp_width, multi_scan, multi_run;
  1456. uint8_t *d;
  1457. uint32_t v, addr1, addr;
  1458. vga_draw_line_func *vga_draw_line;
  1459. full_update |= update_basic_params(s);
  1460. if (!full_update)
  1461. vga_sync_dirty_bitmap(s);
  1462. s->get_resolution(s, &width, &height);
  1463. disp_width = width;
  1464. shift_control = (s->gr[0x05] >> 5) & 3;
  1465. double_scan = (s->cr[0x09] >> 7);
  1466. if (shift_control != 1) {
  1467. multi_scan = (((s->cr[0x09] & 0x1f) + 1) << double_scan) - 1;
  1468. } else {
  1469. /* in CGA modes, multi_scan is ignored */
  1470. /* XXX: is it correct ? */
  1471. multi_scan = double_scan;
  1472. }
  1473. multi_run = multi_scan;
  1474. if (shift_control != s->shift_control ||
  1475. double_scan != s->double_scan) {
  1476. full_update = 1;
  1477. s->shift_control = shift_control;
  1478. s->double_scan = double_scan;
  1479. }
  1480. if (shift_control == 0) {
  1481. if (s->sr[0x01] & 8) {
  1482. disp_width <<= 1;
  1483. }
  1484. } else if (shift_control == 1) {
  1485. if (s->sr[0x01] & 8) {
  1486. disp_width <<= 1;
  1487. }
  1488. }
  1489. depth = s->get_bpp(s);
  1490. if (s->line_offset != s->last_line_offset ||
  1491. disp_width != s->last_width ||
  1492. height != s->last_height ||
  1493. s->last_depth != depth) {
  1494. #if defined(HOST_WORDS_BIGENDIAN) == defined(TARGET_WORDS_BIGENDIAN)
  1495. if (depth == 16 || depth == 32) {
  1496. #else
  1497. if (depth == 32) {
  1498. #endif
  1499. qemu_free_displaysurface(s->ds);
  1500. s->ds->surface = qemu_create_displaysurface_from(disp_width, height, depth,
  1501. s->line_offset,
  1502. s->vram_ptr + (s->start_addr * 4));
  1503. #if defined(HOST_WORDS_BIGENDIAN) != defined(TARGET_WORDS_BIGENDIAN)
  1504. s->ds->surface->pf = qemu_different_endianness_pixelformat(depth);
  1505. #endif
  1506. dpy_resize(s->ds);
  1507. } else {
  1508. qemu_console_resize(s->ds, disp_width, height);
  1509. }
  1510. s->last_scr_width = disp_width;
  1511. s->last_scr_height = height;
  1512. s->last_width = disp_width;
  1513. s->last_height = height;
  1514. s->last_line_offset = s->line_offset;
  1515. s->last_depth = depth;
  1516. full_update = 1;
  1517. } else if (is_buffer_shared(s->ds->surface) &&
  1518. (full_update || s->ds->surface->data != s->vram_ptr + (s->start_addr * 4))) {
  1519. s->ds->surface->data = s->vram_ptr + (s->start_addr * 4);
  1520. dpy_setdata(s->ds);
  1521. }
  1522. s->rgb_to_pixel =
  1523. rgb_to_pixel_dup_table[get_depth_index(s->ds)];
  1524. if (shift_control == 0) {
  1525. full_update |= update_palette16(s);
  1526. if (s->sr[0x01] & 8) {
  1527. v = VGA_DRAW_LINE4D2;
  1528. } else {
  1529. v = VGA_DRAW_LINE4;
  1530. }
  1531. bits = 4;
  1532. } else if (shift_control == 1) {
  1533. full_update |= update_palette16(s);
  1534. if (s->sr[0x01] & 8) {
  1535. v = VGA_DRAW_LINE2D2;
  1536. } else {
  1537. v = VGA_DRAW_LINE2;
  1538. }
  1539. bits = 4;
  1540. } else {
  1541. switch(s->get_bpp(s)) {
  1542. default:
  1543. case 0:
  1544. full_update |= update_palette256(s);
  1545. v = VGA_DRAW_LINE8D2;
  1546. bits = 4;
  1547. break;
  1548. case 8:
  1549. full_update |= update_palette256(s);
  1550. v = VGA_DRAW_LINE8;
  1551. bits = 8;
  1552. break;
  1553. case 15:
  1554. v = VGA_DRAW_LINE15;
  1555. bits = 16;
  1556. break;
  1557. case 16:
  1558. v = VGA_DRAW_LINE16;
  1559. bits = 16;
  1560. break;
  1561. case 24:
  1562. v = VGA_DRAW_LINE24;
  1563. bits = 24;
  1564. break;
  1565. case 32:
  1566. v = VGA_DRAW_LINE32;
  1567. bits = 32;
  1568. break;
  1569. }
  1570. }
  1571. vga_draw_line = vga_draw_line_table[v * NB_DEPTHS + get_depth_index(s->ds)];
  1572. if (!is_buffer_shared(s->ds->surface) && s->cursor_invalidate)
  1573. s->cursor_invalidate(s);
  1574. line_offset = s->line_offset;
  1575. #if 0
  1576. printf("w=%d h=%d v=%d line_offset=%d cr[0x09]=0x%02x cr[0x17]=0x%02x linecmp=%d sr[0x01]=0x%02x\n",
  1577. width, height, v, line_offset, s->cr[9], s->cr[0x17], s->line_compare, s->sr[0x01]);
  1578. #endif
  1579. addr1 = (s->start_addr * 4);
  1580. bwidth = (width * bits + 7) / 8;
  1581. y_start = -1;
  1582. page_min = -1;
  1583. page_max = 0;
  1584. d = ds_get_data(s->ds);
  1585. linesize = ds_get_linesize(s->ds);
  1586. y1 = 0;
  1587. for(y = 0; y < height; y++) {
  1588. addr = addr1;
  1589. if (!(s->cr[0x17] & 1)) {
  1590. int shift;
  1591. /* CGA compatibility handling */
  1592. shift = 14 + ((s->cr[0x17] >> 6) & 1);
  1593. addr = (addr & ~(1 << shift)) | ((y1 & 1) << shift);
  1594. }
  1595. if (!(s->cr[0x17] & 2)) {
  1596. addr = (addr & ~0x8000) | ((y1 & 2) << 14);
  1597. }
  1598. page0 = addr & TARGET_PAGE_MASK;
  1599. page1 = (addr + bwidth - 1) & TARGET_PAGE_MASK;
  1600. update = full_update |
  1601. memory_region_get_dirty(&s->vram, page0, DIRTY_MEMORY_VGA) |
  1602. memory_region_get_dirty(&s->vram, page1, DIRTY_MEMORY_VGA);
  1603. if ((page1 - page0) > TARGET_PAGE_SIZE) {
  1604. /* if wide line, can use another page */
  1605. update |= memory_region_get_dirty(&s->vram,
  1606. page0 + TARGET_PAGE_SIZE,
  1607. DIRTY_MEMORY_VGA);
  1608. }
  1609. /* explicit invalidation for the hardware cursor */
  1610. update |= (s->invalidated_y_table[y >> 5] >> (y & 0x1f)) & 1;
  1611. if (update) {
  1612. if (y_start < 0)
  1613. y_start = y;
  1614. if (page0 < page_min)
  1615. page_min = page0;
  1616. if (page1 > page_max)
  1617. page_max = page1;
  1618. if (!(is_buffer_shared(s->ds->surface))) {
  1619. vga_draw_line(s, d, s->vram_ptr + addr, width);
  1620. if (s->cursor_draw_line)
  1621. s->cursor_draw_line(s, d, y);
  1622. }
  1623. } else {
  1624. if (y_start >= 0) {
  1625. /* flush to display */
  1626. dpy_update(s->ds, 0, y_start,
  1627. disp_width, y - y_start);
  1628. y_start = -1;
  1629. }
  1630. }
  1631. if (!multi_run) {
  1632. mask = (s->cr[0x17] & 3) ^ 3;
  1633. if ((y1 & mask) == mask)
  1634. addr1 += line_offset;
  1635. y1++;
  1636. multi_run = multi_scan;
  1637. } else {
  1638. multi_run--;
  1639. }
  1640. /* line compare acts on the displayed lines */
  1641. if (y == s->line_compare)
  1642. addr1 = 0;
  1643. d += linesize;
  1644. }
  1645. if (y_start >= 0) {
  1646. /* flush to display */
  1647. dpy_update(s->ds, 0, y_start,
  1648. disp_width, y - y_start);
  1649. }
  1650. /* reset modified pages */
  1651. if (page_max >= page_min) {
  1652. memory_region_reset_dirty(&s->vram,
  1653. page_min,
  1654. page_max + TARGET_PAGE_SIZE - page_min,
  1655. DIRTY_MEMORY_VGA);
  1656. }
  1657. memset(s->invalidated_y_table, 0, ((height + 31) >> 5) * 4);
  1658. }
  1659. static void vga_draw_blank(VGACommonState *s, int full_update)
  1660. {
  1661. int i, w, val;
  1662. uint8_t *d;
  1663. if (!full_update)
  1664. return;
  1665. if (s->last_scr_width <= 0 || s->last_scr_height <= 0)
  1666. return;
  1667. s->rgb_to_pixel =
  1668. rgb_to_pixel_dup_table[get_depth_index(s->ds)];
  1669. if (ds_get_bits_per_pixel(s->ds) == 8)
  1670. val = s->rgb_to_pixel(0, 0, 0);
  1671. else
  1672. val = 0;
  1673. w = s->last_scr_width * ((ds_get_bits_per_pixel(s->ds) + 7) >> 3);
  1674. d = ds_get_data(s->ds);
  1675. for(i = 0; i < s->last_scr_height; i++) {
  1676. memset(d, val, w);
  1677. d += ds_get_linesize(s->ds);
  1678. }
  1679. dpy_update(s->ds, 0, 0,
  1680. s->last_scr_width, s->last_scr_height);
  1681. }
  1682. #define GMODE_TEXT 0
  1683. #define GMODE_GRAPH 1
  1684. #define GMODE_BLANK 2
  1685. static void vga_update_display(void *opaque)
  1686. {
  1687. VGACommonState *s = opaque;
  1688. int full_update, graphic_mode;
  1689. qemu_flush_coalesced_mmio_buffer();
  1690. if (ds_get_bits_per_pixel(s->ds) == 0) {
  1691. /* nothing to do */
  1692. } else {
  1693. full_update = 0;
  1694. if (!(s->ar_index & 0x20)) {
  1695. graphic_mode = GMODE_BLANK;
  1696. } else {
  1697. graphic_mode = s->gr[6] & 1;
  1698. }
  1699. if (graphic_mode != s->graphic_mode) {
  1700. s->graphic_mode = graphic_mode;
  1701. full_update = 1;
  1702. }
  1703. switch(graphic_mode) {
  1704. case GMODE_TEXT:
  1705. vga_draw_text(s, full_update);
  1706. break;
  1707. case GMODE_GRAPH:
  1708. vga_draw_graphic(s, full_update);
  1709. break;
  1710. case GMODE_BLANK:
  1711. default:
  1712. vga_draw_blank(s, full_update);
  1713. break;
  1714. }
  1715. }
  1716. }
  1717. /* force a full display refresh */
  1718. static void vga_invalidate_display(void *opaque)
  1719. {
  1720. VGACommonState *s = opaque;
  1721. s->last_width = -1;
  1722. s->last_height = -1;
  1723. }
  1724. void vga_common_reset(VGACommonState *s)
  1725. {
  1726. s->sr_index = 0;
  1727. memset(s->sr, '\0', sizeof(s->sr));
  1728. s->gr_index = 0;
  1729. memset(s->gr, '\0', sizeof(s->gr));
  1730. s->ar_index = 0;
  1731. memset(s->ar, '\0', sizeof(s->ar));
  1732. s->ar_flip_flop = 0;
  1733. s->cr_index = 0;
  1734. memset(s->cr, '\0', sizeof(s->cr));
  1735. s->msr = 0;
  1736. s->fcr = 0;
  1737. s->st00 = 0;
  1738. s->st01 = 0;
  1739. s->dac_state = 0;
  1740. s->dac_sub_index = 0;
  1741. s->dac_read_index = 0;
  1742. s->dac_write_index = 0;
  1743. memset(s->dac_cache, '\0', sizeof(s->dac_cache));
  1744. s->dac_8bit = 0;
  1745. memset(s->palette, '\0', sizeof(s->palette));
  1746. s->bank_offset = 0;
  1747. #ifdef CONFIG_BOCHS_VBE
  1748. s->vbe_index = 0;
  1749. memset(s->vbe_regs, '\0', sizeof(s->vbe_regs));
  1750. s->vbe_regs[VBE_DISPI_INDEX_ID] = VBE_DISPI_ID5;
  1751. s->vbe_start_addr = 0;
  1752. s->vbe_line_offset = 0;
  1753. s->vbe_bank_mask = (s->vram_size >> 16) - 1;
  1754. #endif
  1755. memset(s->font_offsets, '\0', sizeof(s->font_offsets));
  1756. s->graphic_mode = -1; /* force full update */
  1757. s->shift_control = 0;
  1758. s->double_scan = 0;
  1759. s->line_offset = 0;
  1760. s->line_compare = 0;
  1761. s->start_addr = 0;
  1762. s->plane_updated = 0;
  1763. s->last_cw = 0;
  1764. s->last_ch = 0;
  1765. s->last_width = 0;
  1766. s->last_height = 0;
  1767. s->last_scr_width = 0;
  1768. s->last_scr_height = 0;
  1769. s->cursor_start = 0;
  1770. s->cursor_end = 0;
  1771. s->cursor_offset = 0;
  1772. memset(s->invalidated_y_table, '\0', sizeof(s->invalidated_y_table));
  1773. memset(s->last_palette, '\0', sizeof(s->last_palette));
  1774. memset(s->last_ch_attr, '\0', sizeof(s->last_ch_attr));
  1775. switch (vga_retrace_method) {
  1776. case VGA_RETRACE_DUMB:
  1777. break;
  1778. case VGA_RETRACE_PRECISE:
  1779. memset(&s->retrace_info, 0, sizeof (s->retrace_info));
  1780. break;
  1781. }
  1782. vga_update_memory_access(s);
  1783. }
  1784. static void vga_reset(void *opaque)
  1785. {
  1786. VGACommonState *s = opaque;
  1787. vga_common_reset(s);
  1788. }
  1789. #define TEXTMODE_X(x) ((x) % width)
  1790. #define TEXTMODE_Y(x) ((x) / width)
  1791. #define VMEM2CHTYPE(v) ((v & 0xff0007ff) | \
  1792. ((v & 0x00000800) << 10) | ((v & 0x00007000) >> 1))
  1793. /* relay text rendering to the display driver
  1794. * instead of doing a full vga_update_display() */
  1795. static void vga_update_text(void *opaque, console_ch_t *chardata)
  1796. {
  1797. VGACommonState *s = opaque;
  1798. int graphic_mode, i, cursor_offset, cursor_visible;
  1799. int cw, cheight, width, height, size, c_min, c_max;
  1800. uint32_t *src;
  1801. console_ch_t *dst, val;
  1802. char msg_buffer[80];
  1803. int full_update = 0;
  1804. qemu_flush_coalesced_mmio_buffer();
  1805. if (!(s->ar_index & 0x20)) {
  1806. graphic_mode = GMODE_BLANK;
  1807. } else {
  1808. graphic_mode = s->gr[6] & 1;
  1809. }
  1810. if (graphic_mode != s->graphic_mode) {
  1811. s->graphic_mode = graphic_mode;
  1812. full_update = 1;
  1813. }
  1814. if (s->last_width == -1) {
  1815. s->last_width = 0;
  1816. full_update = 1;
  1817. }
  1818. switch (graphic_mode) {
  1819. case GMODE_TEXT:
  1820. /* TODO: update palette */
  1821. full_update |= update_basic_params(s);
  1822. /* total width & height */
  1823. cheight = (s->cr[9] & 0x1f) + 1;
  1824. cw = 8;
  1825. if (!(s->sr[1] & 0x01))
  1826. cw = 9;
  1827. if (s->sr[1] & 0x08)
  1828. cw = 16; /* NOTE: no 18 pixel wide */
  1829. width = (s->cr[0x01] + 1);
  1830. if (s->cr[0x06] == 100) {
  1831. /* ugly hack for CGA 160x100x16 - explain me the logic */
  1832. height = 100;
  1833. } else {
  1834. height = s->cr[0x12] |
  1835. ((s->cr[0x07] & 0x02) << 7) |
  1836. ((s->cr[0x07] & 0x40) << 3);
  1837. height = (height + 1) / cheight;
  1838. }
  1839. size = (height * width);
  1840. if (size > CH_ATTR_SIZE) {
  1841. if (!full_update)
  1842. return;
  1843. snprintf(msg_buffer, sizeof(msg_buffer), "%i x %i Text mode",
  1844. width, height);
  1845. break;
  1846. }
  1847. if (width != s->last_width || height != s->last_height ||
  1848. cw != s->last_cw || cheight != s->last_ch) {
  1849. s->last_scr_width = width * cw;
  1850. s->last_scr_height = height * cheight;
  1851. s->ds->surface->width = width;
  1852. s->ds->surface->height = height;
  1853. dpy_resize(s->ds);
  1854. s->last_width = width;
  1855. s->last_height = height;
  1856. s->last_ch = cheight;
  1857. s->last_cw = cw;
  1858. full_update = 1;
  1859. }
  1860. /* Update "hardware" cursor */
  1861. cursor_offset = ((s->cr[0x0e] << 8) | s->cr[0x0f]) - s->start_addr;
  1862. if (cursor_offset != s->cursor_offset ||
  1863. s->cr[0xa] != s->cursor_start ||
  1864. s->cr[0xb] != s->cursor_end || full_update) {
  1865. cursor_visible = !(s->cr[0xa] & 0x20);
  1866. if (cursor_visible && cursor_offset < size && cursor_offset >= 0)
  1867. dpy_cursor(s->ds,
  1868. TEXTMODE_X(cursor_offset),
  1869. TEXTMODE_Y(cursor_offset));
  1870. else
  1871. dpy_cursor(s->ds, -1, -1);
  1872. s->cursor_offset = cursor_offset;
  1873. s->cursor_start = s->cr[0xa];
  1874. s->cursor_end = s->cr[0xb];
  1875. }
  1876. src = (uint32_t *) s->vram_ptr + s->start_addr;
  1877. dst = chardata;
  1878. if (full_update) {
  1879. for (i = 0; i < size; src ++, dst ++, i ++)
  1880. console_write_ch(dst, VMEM2CHTYPE(le32_to_cpu(*src)));
  1881. dpy_update(s->ds, 0, 0, width, height);
  1882. } else {
  1883. c_max = 0;
  1884. for (i = 0; i < size; src ++, dst ++, i ++) {
  1885. console_write_ch(&val, VMEM2CHTYPE(le32_to_cpu(*src)));
  1886. if (*dst != val) {
  1887. *dst = val;
  1888. c_max = i;
  1889. break;
  1890. }
  1891. }
  1892. c_min = i;
  1893. for (; i < size; src ++, dst ++, i ++) {
  1894. console_write_ch(&val, VMEM2CHTYPE(le32_to_cpu(*src)));
  1895. if (*dst != val) {
  1896. *dst = val;
  1897. c_max = i;
  1898. }
  1899. }
  1900. if (c_min <= c_max) {
  1901. i = TEXTMODE_Y(c_min);
  1902. dpy_update(s->ds, 0, i, width, TEXTMODE_Y(c_max) - i + 1);
  1903. }
  1904. }
  1905. return;
  1906. case GMODE_GRAPH:
  1907. if (!full_update)
  1908. return;
  1909. s->get_resolution(s, &width, &height);
  1910. snprintf(msg_buffer, sizeof(msg_buffer), "%i x %i Graphic mode",
  1911. width, height);
  1912. break;
  1913. case GMODE_BLANK:
  1914. default:
  1915. if (!full_update)
  1916. return;
  1917. snprintf(msg_buffer, sizeof(msg_buffer), "VGA Blank mode");
  1918. break;
  1919. }
  1920. /* Display a message */
  1921. s->last_width = 60;
  1922. s->last_height = height = 3;
  1923. dpy_cursor(s->ds, -1, -1);
  1924. s->ds->surface->width = s->last_width;
  1925. s->ds->surface->height = height;
  1926. dpy_resize(s->ds);
  1927. for (dst = chardata, i = 0; i < s->last_width * height; i ++)
  1928. console_write_ch(dst ++, ' ');
  1929. size = strlen(msg_buffer);
  1930. width = (s->last_width - size) / 2;
  1931. dst = chardata + s->last_width + width;
  1932. for (i = 0; i < size; i ++)
  1933. console_write_ch(dst ++, 0x00200100 | msg_buffer[i]);
  1934. dpy_update(s->ds, 0, 0, s->last_width, height);
  1935. }
  1936. static uint64_t vga_mem_read(void *opaque, target_phys_addr_t addr,
  1937. unsigned size)
  1938. {
  1939. VGACommonState *s = opaque;
  1940. return vga_mem_readb(s, addr);
  1941. }
  1942. static void vga_mem_write(void *opaque, target_phys_addr_t addr,
  1943. uint64_t data, unsigned size)
  1944. {
  1945. VGACommonState *s = opaque;
  1946. return vga_mem_writeb(s, addr, data);
  1947. }
  1948. const MemoryRegionOps vga_mem_ops = {
  1949. .read = vga_mem_read,
  1950. .write = vga_mem_write,
  1951. .endianness = DEVICE_LITTLE_ENDIAN,
  1952. .impl = {
  1953. .min_access_size = 1,
  1954. .max_access_size = 1,
  1955. },
  1956. };
  1957. static int vga_common_post_load(void *opaque, int version_id)
  1958. {
  1959. VGACommonState *s = opaque;
  1960. /* force refresh */
  1961. s->graphic_mode = -1;
  1962. return 0;
  1963. }
  1964. const VMStateDescription vmstate_vga_common = {
  1965. .name = "vga",
  1966. .version_id = 2,
  1967. .minimum_version_id = 2,
  1968. .minimum_version_id_old = 2,
  1969. .post_load = vga_common_post_load,
  1970. .fields = (VMStateField []) {
  1971. VMSTATE_UINT32(latch, VGACommonState),
  1972. VMSTATE_UINT8(sr_index, VGACommonState),
  1973. VMSTATE_PARTIAL_BUFFER(sr, VGACommonState, 8),
  1974. VMSTATE_UINT8(gr_index, VGACommonState),
  1975. VMSTATE_PARTIAL_BUFFER(gr, VGACommonState, 16),
  1976. VMSTATE_UINT8(ar_index, VGACommonState),
  1977. VMSTATE_BUFFER(ar, VGACommonState),
  1978. VMSTATE_INT32(ar_flip_flop, VGACommonState),
  1979. VMSTATE_UINT8(cr_index, VGACommonState),
  1980. VMSTATE_BUFFER(cr, VGACommonState),
  1981. VMSTATE_UINT8(msr, VGACommonState),
  1982. VMSTATE_UINT8(fcr, VGACommonState),
  1983. VMSTATE_UINT8(st00, VGACommonState),
  1984. VMSTATE_UINT8(st01, VGACommonState),
  1985. VMSTATE_UINT8(dac_state, VGACommonState),
  1986. VMSTATE_UINT8(dac_sub_index, VGACommonState),
  1987. VMSTATE_UINT8(dac_read_index, VGACommonState),
  1988. VMSTATE_UINT8(dac_write_index, VGACommonState),
  1989. VMSTATE_BUFFER(dac_cache, VGACommonState),
  1990. VMSTATE_BUFFER(palette, VGACommonState),
  1991. VMSTATE_INT32(bank_offset, VGACommonState),
  1992. VMSTATE_UINT8_EQUAL(is_vbe_vmstate, VGACommonState),
  1993. #ifdef CONFIG_BOCHS_VBE
  1994. VMSTATE_UINT16(vbe_index, VGACommonState),
  1995. VMSTATE_UINT16_ARRAY(vbe_regs, VGACommonState, VBE_DISPI_INDEX_NB),
  1996. VMSTATE_UINT32(vbe_start_addr, VGACommonState),
  1997. VMSTATE_UINT32(vbe_line_offset, VGACommonState),
  1998. VMSTATE_UINT32(vbe_bank_mask, VGACommonState),
  1999. #endif
  2000. VMSTATE_END_OF_LIST()
  2001. }
  2002. };
  2003. void vga_common_init(VGACommonState *s, int vga_ram_size)
  2004. {
  2005. int i, j, v, b;
  2006. for(i = 0;i < 256; i++) {
  2007. v = 0;
  2008. for(j = 0; j < 8; j++) {
  2009. v |= ((i >> j) & 1) << (j * 4);
  2010. }
  2011. expand4[i] = v;
  2012. v = 0;
  2013. for(j = 0; j < 4; j++) {
  2014. v |= ((i >> (2 * j)) & 3) << (j * 4);
  2015. }
  2016. expand2[i] = v;
  2017. }
  2018. for(i = 0; i < 16; i++) {
  2019. v = 0;
  2020. for(j = 0; j < 4; j++) {
  2021. b = ((i >> j) & 1);
  2022. v |= b << (2 * j);
  2023. v |= b << (2 * j + 1);
  2024. }
  2025. expand4to8[i] = v;
  2026. }
  2027. #ifdef CONFIG_BOCHS_VBE
  2028. s->is_vbe_vmstate = 1;
  2029. #else
  2030. s->is_vbe_vmstate = 0;
  2031. #endif
  2032. memory_region_init_ram(&s->vram, NULL, "vga.vram", vga_ram_size);
  2033. s->vram_ptr = memory_region_get_ram_ptr(&s->vram);
  2034. s->vram_size = vga_ram_size;
  2035. s->get_bpp = vga_get_bpp;
  2036. s->get_offsets = vga_get_offsets;
  2037. s->get_resolution = vga_get_resolution;
  2038. s->update = vga_update_display;
  2039. s->invalidate = vga_invalidate_display;
  2040. s->screen_dump = vga_screen_dump;
  2041. s->text_update = vga_update_text;
  2042. switch (vga_retrace_method) {
  2043. case VGA_RETRACE_DUMB:
  2044. s->retrace = vga_dumb_retrace;
  2045. s->update_retrace_info = vga_dumb_update_retrace_info;
  2046. break;
  2047. case VGA_RETRACE_PRECISE:
  2048. s->retrace = vga_precise_retrace;
  2049. s->update_retrace_info = vga_precise_update_retrace_info;
  2050. break;
  2051. }
  2052. vga_dirty_log_start(s);
  2053. }
  2054. static const MemoryRegionPortio vga_portio_list[] = {
  2055. { 0x04, 2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3b4 */
  2056. { 0x0a, 1, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3ba */
  2057. { 0x10, 16, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3c0 */
  2058. { 0x24, 2, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3d4 */
  2059. { 0x2a, 1, 1, .read = vga_ioport_read, .write = vga_ioport_write }, /* 3da */
  2060. PORTIO_END_OF_LIST(),
  2061. };
  2062. #ifdef CONFIG_BOCHS_VBE
  2063. static const MemoryRegionPortio vbe_portio_list[] = {
  2064. { 0, 1, 2, .read = vbe_ioport_read_index, .write = vbe_ioport_write_index },
  2065. # ifdef TARGET_I386
  2066. { 1, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
  2067. # else
  2068. { 2, 1, 2, .read = vbe_ioport_read_data, .write = vbe_ioport_write_data },
  2069. # endif
  2070. PORTIO_END_OF_LIST(),
  2071. };
  2072. #endif /* CONFIG_BOCHS_VBE */
  2073. /* Used by both ISA and PCI */
  2074. MemoryRegion *vga_init_io(VGACommonState *s,
  2075. const MemoryRegionPortio **vga_ports,
  2076. const MemoryRegionPortio **vbe_ports)
  2077. {
  2078. MemoryRegion *vga_mem;
  2079. *vga_ports = vga_portio_list;
  2080. *vbe_ports = NULL;
  2081. #ifdef CONFIG_BOCHS_VBE
  2082. *vbe_ports = vbe_portio_list;
  2083. #endif
  2084. vga_mem = g_malloc(sizeof(*vga_mem));
  2085. memory_region_init_io(vga_mem, &vga_mem_ops, s,
  2086. "vga-lowmem", 0x20000);
  2087. return vga_mem;
  2088. }
  2089. void vga_init(VGACommonState *s, MemoryRegion *address_space,
  2090. MemoryRegion *address_space_io, bool init_vga_ports)
  2091. {
  2092. MemoryRegion *vga_io_memory;
  2093. const MemoryRegionPortio *vga_ports, *vbe_ports;
  2094. PortioList *vga_port_list = g_new(PortioList, 1);
  2095. PortioList *vbe_port_list = g_new(PortioList, 1);
  2096. qemu_register_reset(vga_reset, s);
  2097. s->bank_offset = 0;
  2098. s->legacy_address_space = address_space;
  2099. vga_io_memory = vga_init_io(s, &vga_ports, &vbe_ports);
  2100. memory_region_add_subregion_overlap(address_space,
  2101. isa_mem_base + 0x000a0000,
  2102. vga_io_memory,
  2103. 1);
  2104. memory_region_set_coalescing(vga_io_memory);
  2105. if (init_vga_ports) {
  2106. portio_list_init(vga_port_list, vga_ports, s, "vga");
  2107. portio_list_add(vga_port_list, address_space_io, 0x3b0);
  2108. }
  2109. if (vbe_ports) {
  2110. portio_list_init(vbe_port_list, vbe_ports, s, "vbe");
  2111. portio_list_add(vbe_port_list, address_space_io, 0x1ce);
  2112. }
  2113. }
  2114. void vga_init_vbe(VGACommonState *s, MemoryRegion *system_memory)
  2115. {
  2116. #ifdef CONFIG_BOCHS_VBE
  2117. /* XXX: use optimized standard vga accesses */
  2118. memory_region_add_subregion(system_memory,
  2119. VBE_DISPI_LFB_PHYSICAL_ADDRESS,
  2120. &s->vram);
  2121. s->vbe_mapped = 1;
  2122. #endif
  2123. }
  2124. /********************************************************/
  2125. /* vga screen dump */
  2126. static void vga_save_dpy_update(DisplayState *ds,
  2127. int x, int y, int w, int h)
  2128. {
  2129. if (screen_dump_filename) {
  2130. ppm_save(screen_dump_filename, ds->surface);
  2131. }
  2132. }
  2133. static void vga_save_dpy_resize(DisplayState *s)
  2134. {
  2135. }
  2136. static void vga_save_dpy_refresh(DisplayState *s)
  2137. {
  2138. }
  2139. int ppm_save(const char *filename, struct DisplaySurface *ds)
  2140. {
  2141. FILE *f;
  2142. uint8_t *d, *d1;
  2143. uint32_t v;
  2144. int y, x;
  2145. uint8_t r, g, b;
  2146. int ret;
  2147. char *linebuf, *pbuf;
  2148. f = fopen(filename, "wb");
  2149. if (!f)
  2150. return -1;
  2151. fprintf(f, "P6\n%d %d\n%d\n",
  2152. ds->width, ds->height, 255);
  2153. linebuf = g_malloc(ds->width * 3);
  2154. d1 = ds->data;
  2155. for(y = 0; y < ds->height; y++) {
  2156. d = d1;
  2157. pbuf = linebuf;
  2158. for(x = 0; x < ds->width; x++) {
  2159. if (ds->pf.bits_per_pixel == 32)
  2160. v = *(uint32_t *)d;
  2161. else
  2162. v = (uint32_t) (*(uint16_t *)d);
  2163. r = ((v >> ds->pf.rshift) & ds->pf.rmax) * 256 /
  2164. (ds->pf.rmax + 1);
  2165. g = ((v >> ds->pf.gshift) & ds->pf.gmax) * 256 /
  2166. (ds->pf.gmax + 1);
  2167. b = ((v >> ds->pf.bshift) & ds->pf.bmax) * 256 /
  2168. (ds->pf.bmax + 1);
  2169. *pbuf++ = r;
  2170. *pbuf++ = g;
  2171. *pbuf++ = b;
  2172. d += ds->pf.bytes_per_pixel;
  2173. }
  2174. d1 += ds->linesize;
  2175. ret = fwrite(linebuf, 1, pbuf - linebuf, f);
  2176. (void)ret;
  2177. }
  2178. g_free(linebuf);
  2179. fclose(f);
  2180. return 0;
  2181. }
  2182. static DisplayChangeListener* vga_screen_dump_init(DisplayState *ds)
  2183. {
  2184. DisplayChangeListener *dcl;
  2185. dcl = g_malloc0(sizeof(DisplayChangeListener));
  2186. dcl->dpy_update = vga_save_dpy_update;
  2187. dcl->dpy_resize = vga_save_dpy_resize;
  2188. dcl->dpy_refresh = vga_save_dpy_refresh;
  2189. register_displaychangelistener(ds, dcl);
  2190. return dcl;
  2191. }
  2192. /* save the vga display in a PPM image even if no display is
  2193. available */
  2194. static void vga_screen_dump(void *opaque, const char *filename)
  2195. {
  2196. VGACommonState *s = opaque;
  2197. if (!screen_dump_dcl)
  2198. screen_dump_dcl = vga_screen_dump_init(s->ds);
  2199. screen_dump_filename = filename;
  2200. vga_invalidate_display(s);
  2201. vga_hw_update();
  2202. screen_dump_filename = NULL;
  2203. }