10 #endif /* HAVE_LIBPNG */
12 #ifdef HAVE_LIBVNCSERVER
14 #include <rfb/keysym.h>
15 #endif /* HAVE_LIBVNCSERVER */
18 #include "chargen-4x8.h"
19 #include "hw_lem1802.h"
22 #define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0)
24 #define TRACE(...) do {} while (0)
27 #ifdef WANT_VARIADIC_VOIDP_CAST
28 #define VOIDP(__x__) ((void *)(__x__))
29 #define VOIDFP(__x__) ((void *)(size_t)(__x__))
31 #define VOIDP(__x__) (__x__)
32 #define VOIDFP(__x__) (__x__)
35 #define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */
37 #define PIX_X 160 /* pixels in display */
38 #define PIX_Y 128 /* pixels in display */
39 #define PIX_BORDER 16 /* border pixels from edge to first tile */
42 #define CELL_X ((PIX_X - (2 * PIX_BORDER)) / CELL_X_SZ) /* tiles in display */
43 #define CELL_Y ((PIX_Y - (2 * PIX_BORDER)) / CELL_Y_SZ) /* tiles in display */
45 #define PALETTE_ENTRIES 16
46 static const DCPU16_WORD palette_default_
[PALETTE_ENTRIES
] = {
54 0x0aaf, /* pale blue */
56 0x055f, /* light blue */
57 0x05f5, /* light green*/
58 0x05ff, /* light cyan */
59 0x0f55, /* light red */
60 0x0f5f, /* light magenta */
61 0x0ff5, /* light yellow */
73 long long cycle_activated
; /* running since */
74 long long cycles_until_active_
; /* for tracking power-up delay */
76 DCPU16_WORD video_base
;
77 DCPU16_WORD font_base
;
78 DCPU16_WORD palette_base
;
79 DCPU16_WORD border_color
;
80 struct pixel_
*pixbuf
;
82 unsigned int refresh_rate
; /* redraw every n cycles */
83 unsigned int refresh_tally_
; /* tick */
85 unsigned int blink_rate
; /* toggle every n cycles? still figuring this out.. */
86 unsigned int blink_tally_
; /* tick */
87 unsigned int blink_state
;
93 const DCPU16_WORD
*cycle_state_copy_src_ptr_
;
94 DCPU16_WORD cycle_state_copy_dst_addr_
;
95 size_t cycle_state_copy_words_
;
97 int (*render
)(void *, struct pixel_
*, size_t, size_t);
102 long long power_on_cycles_(void) {
107 gettimeofday(&tv
, NULL
);
108 r
+= LEM1802_POWER_ON_CYCLES
- (LEM1802_POWER_ON_CYCLES
/ 10);
109 r
+= tv
.tv_usec
% (LEM1802_POWER_ON_CYCLES
/ 5);
116 void pixel_color_(struct pixel_
*pix
, DCPU16_WORD color
) {
119 x
= (color
>> 0) & 0x000f;
120 pix
->r
= x
| (x
<< 4);
122 x
= (color
>> 4) & 0x000f;
123 pix
->g
= x
| (x
<< 4);
125 x
= (color
>> 8) & 0x000f;
126 pix
->b
= x
| (x
<< 4);
128 x
= (color
>> 12) & 0x000f;
129 pix
->a
= x
| (x
<< 4);
133 void pixbuf_border_paint_(struct pixel_
*pixbuf
, struct pixel_
*border
) {
136 TRACE("%s>> painted border", __func__
);
139 for (y
= 0; y
< PIX_BORDER
; y
++) {
140 for (x
= 0; x
< PIX_X
; x
++) {
143 i
= ((PIX_Y
- y
) * PIX_X
) + x
;
149 for (y
= PIX_BORDER
; y
< (PIX_Y
- PIX_BORDER
+ 1); y
++)
150 for (x
= 0; x
< PIX_BORDER
; x
++) {
153 pixbuf
[i
+ (PIX_X
- PIX_BORDER
)] = *border
;
158 void font_tile_paint_(struct pixel_
*p
, struct pixel_
*fg
, struct pixel_
*bg
, DCPU16_WORD
*tile
) {
160 unsigned char *font_bitmap
= (unsigned char *)tile
;
163 TRACE("%s>> fg:(%u,%u,%u) bg:(%u,%u,%u) font_bitmap:%02x %02x %02x %02x", __func__
,
166 font_bitmap
[0], font_bitmap
[1], font_bitmap
[2], font_bitmap
[3]);
169 for (pix_x
= 0; pix_x
< CELL_X_SZ
; pix_x
++) {
170 for (pix_y
= 0; pix_y
< CELL_Y_SZ
; pix_y
++) {
171 if ((font_bitmap
[pix_x
] >> pix_y
) & 0x01)
172 p
[((CELL_Y_SZ
- pix_y
- 1) * PIX_X
) + pix_x
] = *fg
;
174 p
[((CELL_Y_SZ
- pix_y
- 1) * PIX_X
) + pix_x
] = *bg
;
180 void pixbuf_addr_paint_(struct pixel_
*pixbuf
, DCPU16_WORD
*mem
, DCPU16_WORD base
, DCPU16_WORD addr
, DCPU16_WORD
*palette
, DCPU16_WORD
*tiles
, unsigned int blink_state
) {
181 struct pixel_
*tilestart
= pixbuf
; /* start of display */
182 unsigned int cell_x
= addr
% (PIX_X
/ CELL_X_SZ
),
183 cell_y
= addr
/ (PIX_X
/ CELL_X_SZ
);
184 struct pixel_ fg
, bg
;
185 DCPU16_WORD
*font_bitmap
;
188 cell_x
= (addr
- base
) % CELL_X
;
189 cell_y
= (addr
- base
) / CELL_X
;
192 TRACE("%s>> addr:0x%04x col:%u row:%u v:%hu",
195 cell_x
, cell_y
, mem
[addr
]);
198 blink
= mem
[addr
] & 0x0080;
200 /* tiles take two words each */
201 font_bitmap
= tiles
+ (2 * (mem
[addr
] & 0x7f));
203 pixel_color_(&bg
, palette
[(mem
[addr
] >> 8) & 0x0f]);
204 if (blink
&& blink_state
)
205 pixel_color_(&fg
, palette
[(mem
[addr
] >> 8) & 0x0f]);
207 pixel_color_(&fg
, palette
[(mem
[addr
] >> 12) & 0x0f]);
209 tilestart
+= (PIX_X
* PIX_BORDER
); /* skip top border */
210 tilestart
+= (CELL_Y_SZ
* PIX_X
) * cell_y
; /* skip down to row */
212 tilestart
+= PIX_BORDER
; /* skip side border */
213 tilestart
+= (CELL_X_SZ
) * cell_x
; /* skip to column */
215 font_tile_paint_(tilestart
, &fg
, &bg
, font_bitmap
);
219 void lem1802_pixbuf_refresh_full_(struct lem1802_
*display
, DCPU16_WORD
*mem
) {
220 struct pixel_ border
;
224 TRACE("%s>> video_base:0x%04x", __func__
, display
->video_base
);
227 if (display
->cycles_until_active_
) {
228 /* show cute power-up sequence.. */
229 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
233 if (display
->video_base
== 0) {
234 /* disconnected, blank display */
235 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
239 pixel_color_(&border
, display
->border_color
);
240 pixbuf_border_paint_(display
->pixbuf
, &border
);
242 for (tile
= 0; tile
< CELL_X
* CELL_Y
; tile
++) {
243 pixbuf_addr_paint_(display
->pixbuf
,
246 display
->video_base
+ tile
,
247 display
->palette_base
? mem
+ display
->palette_base
: (DCPU16_WORD
*)palette_default_
,
248 display
->font_base
? mem
+ display
->font_base
: (DCPU16_WORD
*)chargen_4x8_glyphs
,
249 display
->blink_state
);
254 int pixbuf_render_pnm_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
255 FILE *f
= (FILE *)data
;
261 for (i
= 0; i
< x
* y
; i
++) {
262 fwrite(&pixbuf
[i
].r
, 1, 1, f
);
263 fwrite(&pixbuf
[i
].g
, 1, 1, f
);
264 fwrite(&pixbuf
[i
].b
, 1, 1, f
);
273 int pixbuf_render_png_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
274 FILE *f
= (FILE *)data
;
280 png
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
285 info
= png_create_info_struct(png
);
287 png_destroy_write_struct(&png
, (png_infopp
)NULL
);
291 if (setjmp(png_jmpbuf(png
))) {
292 png_destroy_write_struct(&png
, &info
);
297 png_set_IHDR(png
, info
, x
, y
, 8, PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
298 png_set_invert_alpha(png
);
299 png_write_info(png
, info
);
300 for (i
= 0; i
< y
; i
++) {
301 png_write_row(png
, (unsigned char *)(pixbuf
+ (i
* x
)));
303 png_write_end(png
, info
);
305 png_destroy_write_struct(&png
, &info
);
312 #endif /* HAVE_LIBPNG */
314 #ifdef HAVE_LIBVNCSERVER
315 /* create and return a new screen, easiest to do here because we know the screen dimensions */
316 rfbScreenInfoPtr
lem1802_rfb_new(int argc
, char *argv
[]) {
318 int paddedWidth
= PIX_X
+ ( (PIX_X
& 3) ? (4 - (PIX_X
& 3)) : 0 );
320 int bitsPerSample
= 8;
321 int samplesPerPixel
= 3;
322 int bytesPerPixel
= 4;
324 s
= rfbGetScreen(&argc
, argv
, paddedWidth
, height
, bitsPerSample
, samplesPerPixel
, bytesPerPixel
);
328 s
->alwaysShared
= TRUE
;
331 s
->httpDir
= "../classes";
334 TRACE("%s>> s:%p", __func__
, VOIDP(s
));
335 TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__
, VOIDFP(s
->kbdAddEvent
), s
->frameBuffer
);
340 /* set up a new screen to see our pixels */
341 void lem1802_vnc_associate(struct dcpu16_hw
*hw
, rfbScreenInfoPtr s
) {
342 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
344 s
->desktopName
= "NYA ELEKTRISKA LEM1802";
345 s
->frameBuffer
= (char *)display
->pixbuf
;
347 TRACE("%s>> s:%p\n", __func__
, VOIDP(s
));
350 /* notify rfb server that pixels may have changed */
352 int pixbuf_render_vnc_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
353 rfbScreenInfoPtr s
= (rfbScreenInfoPtr
)data
;
360 rfbMarkRectAsModified(s
, 0, 0, x
, y
);
362 TRACE("%s>>", __func__
);
366 #endif /* HAVE_LIBVNCSERVER */
370 void lem1802_reset_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
371 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
375 display
->cycle_activated
= 0;
376 display
->video_base
= 0;
377 display
->font_base
= 0;
378 display
->palette_base
= 0;
379 display
->border_color
= 0;
381 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
383 display
->refresh_tally_
= 0;
384 display
->blink_tally_
= 0;
385 display
->blink_state
= 0;
387 display
->cycle_state_
= 0;
390 vm
->trace_cb_("%s>>", __func__
);
395 void lem1802_cycle_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
396 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
400 for more cycle-accuracy, could step through video memory, if set,
402 for now just count cycles and issue a full refresh/render
406 display
->blink_tally_
++;
407 if (display
->blink_tally_
>= display
->blink_rate
) {
408 display
->blink_tally_
= 0;
409 display
->blink_state
^= 1;
410 TRACE("%s>> blink:%u (%u cycles)", __func__
, display
->blink_state
, display
->blink_rate
);
413 display
->refresh_tally_
++;
414 if (display
->refresh_tally_
>= display
->refresh_rate
) {
415 display
->refresh_tally_
= 0;
417 TRACE("%s>> refresh", __func__
);
418 lem1802_pixbuf_refresh_full_(display
, vm
->ram
);
419 display
->render(display
->renderer_data
, display
->pixbuf
, PIX_X
, PIX_Y
);
422 switch (display
->cycle_state_
) {
426 case CYCLE_COPY_TO_RAM
:
427 TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x",
429 display
->cycle_state_copy_words_
,
430 VOIDP(display
->cycle_state_copy_src_ptr_
),
431 display
->cycle_state_copy_dst_addr_
);
432 vm
->ram
[display
->cycle_state_copy_dst_addr_
] = *display
->cycle_state_copy_src_ptr_
;
433 display
->cycle_state_copy_dst_addr_
++;
434 display
->cycle_state_copy_src_ptr_
++;
435 display
->cycle_state_copy_words_
--;
436 if (display
->cycle_state_copy_words_
== 0) {
437 display
->cycle_state_
= CYCLE_IDLE
;
442 if (display
->cycles_until_active_
) {
443 display
->cycles_until_active_
--;
444 if (display
->cycles_until_active_
== 0) {
445 TRACE("%s>> display now active", __func__
);
451 void lem1802_hwi_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
452 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
453 DCPU16_WORD reg_a
= vm
->reg
[DCPU16_REG_A
];
454 DCPU16_WORD reg_b
= vm
->reg
[DCPU16_REG_B
];
456 TRACE("%s>> A:0x%04x B:0x%04x", __func__
, reg_a
, reg_b
);
459 case 0: /* MEM_MAP_SCREEN */
460 if (display
->cycle_activated
== 0 && reg_b
) {
461 display
->cycle_activated
= vm
->cycle_
;
462 display
->cycles_until_active_
= power_on_cycles_();
464 display
->video_base
= reg_b
;
466 display
->cycle_activated
= 0;
469 case 1: /* MEM_MAP_FONT */
470 display
->font_base
= reg_b
;
473 case 2: /* MEM_MAP_PALETTE */
474 display
->palette_base
= reg_b
;
477 case 3: /* SET_BORDER_COLOR */
478 display
->border_color
= reg_b
& 0x000f;
481 case 4: /* MEM_DUMP_FONT */
482 display
->cycle_state_copy_src_ptr_
= (DCPU16_WORD
*)chargen_4x8_glyphs
;
483 display
->cycle_state_copy_dst_addr_
= reg_b
;
484 display
->cycle_state_copy_words_
= 256;
485 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
486 dcpu16_cycle_inc(vm
, 256);
489 case 5: /* MEM_DUMP_PALETTE */
490 display
->cycle_state_copy_src_ptr_
= palette_default_
;
491 display
->cycle_state_copy_dst_addr_
= reg_b
;
492 display
->cycle_state_copy_words_
= 16;
493 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
494 dcpu16_cycle_inc(vm
, 16);
499 static struct renderer_
{
502 int (*renderer
)(void *, struct pixel_
*, size_t, size_t);
503 } lem1802_renderers_
[] = {
504 { "pnm", "filename", pixbuf_render_pnm_
},
506 { "png", "filename", pixbuf_render_png_
},
507 #endif /* HAVE_LIBPNG */
508 #ifdef HAVE_LIBVNCSERVER
509 { "vnc", "", pixbuf_render_vnc_
},
510 #endif /* HAVE_LIBVNCSERVER */
511 { "none", "", NULL
},
515 int lem1802_renderer_set(struct dcpu16_hw
*hw
, const char *renderer
, void *data
) {
518 for (r
= lem1802_renderers_
; r
->renderer
; r
++) {
519 if (strcmp(renderer
, r
->name
) == 0) {
520 ((struct lem1802_
*)(hw
->data
))->render
= r
->renderer
;
521 ((struct lem1802_
*)(hw
->data
))->renderer_data
= data
;
522 TRACE("%s>> renderer set to %s", __func__
, renderer
);
530 char *lem1802_renderers_iter(void **iterp
, char **name
, char **args
) {
531 struct renderer_
**r
= (struct renderer_
**)iterp
;
534 *r
= lem1802_renderers_
;
538 if ((*r
)->name
== NULL
) {
549 int lem1802_data_init_(struct dcpu16_hw
*hw
, void *data
) {
552 hw
->data
= calloc(1, sizeof(struct lem1802_
));
553 if (hw
->data
== NULL
) {
554 hw
->vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
558 ((struct lem1802_
*)(hw
->data
))->pixbuf
= calloc(PIX_X
* PIX_Y
, sizeof *((struct lem1802_
*)(hw
->data
))->pixbuf
);
559 if (((struct lem1802_
*)(hw
->data
))->pixbuf
== NULL
) {
560 hw
->vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
566 ((struct lem1802_
*)(hw
->data
))->refresh_rate
= 1666;
567 ((struct lem1802_
*)(hw
->data
))->blink_rate
= 75000;
572 void lem1802_data_free_(struct dcpu16_hw
*hw
) {
575 /* FIXME: free renderer data */
576 hw
->vm
->warn_cb_("FIXME");
578 if (((struct lem1802_
*)(hw
->data
))->pixbuf
) {
579 free(((struct lem1802_
*)(hw
->data
))->pixbuf
);
580 ((struct lem1802_
*)(hw
->data
))->pixbuf
= NULL
;
590 static struct dcpu16_hw hw_
= {
591 .name_
= "LEM1802 - Low Energy Monitor",
598 .cycle
= lem1802_cycle_
,
599 .reset
= lem1802_reset_
,
600 .data
= (struct lem1802_
*)NULL
603 struct dcpu16_hw_module dcpu16_hw_module_lem1802
= {
605 .data_init
= lem1802_data_init_
,
606 .data_free
= lem1802_data_free_
,