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"
24 * multiple vnc displays
28 #define TRACE(...) do { printf("[debug] "); printf(__VA_ARGS__); printf("\n"); } while (0)
30 #define TRACE(...) do {} while (0)
33 #ifdef WANT_VARIADIC_VOIDP_CAST
34 #define VOIDP(__x__) ((void *)(__x__))
35 #define VOIDFP(__x__) ((void *)(size_t)(__x__))
37 #define VOIDP(__x__) (__x__)
38 #define VOIDFP(__x__) (__x__)
41 #define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */
43 #define PIX_X 160 /* pixels in display */
44 #define PIX_Y 128 /* pixels in display */
45 #define PIX_BORDER 16 /* border pixels from edge to first tile */
48 #define CELL_X ((PIX_X - (2 * PIX_BORDER)) / CELL_X_SZ) /* tiles in display */
49 #define CELL_Y ((PIX_Y - (2 * PIX_BORDER)) / CELL_Y_SZ) /* tiles in display */
51 #define PALETTE_ENTRIES 16
52 static const DCPU16_WORD palette_default_
[PALETTE_ENTRIES
] = {
60 0x0aaf, /* pale blue */
62 0x055f, /* light blue */
63 0x05f5, /* light green*/
64 0x05ff, /* light cyan */
65 0x0f55, /* light red */
66 0x0f5f, /* light magenta */
67 0x0ff5, /* light yellow */
79 long long cycle_activated
; /* running since */
80 long long cycles_until_active_
; /* for tracking power-up delay */
82 DCPU16_WORD video_base
;
83 DCPU16_WORD font_base
;
84 DCPU16_WORD palette_base
;
85 DCPU16_WORD border_color
;
86 struct pixel_
*pixbuf
;
88 unsigned int refresh_rate
; /* redraw every n cycles */
89 unsigned int refresh_tally_
; /* tick */
91 unsigned int blink_rate
; /* toggle every n cycles? still figuring this out.. */
92 unsigned int blink_tally_
; /* tick */
93 unsigned int blink_state
;
99 const DCPU16_WORD
*cycle_state_copy_src_ptr_
;
100 DCPU16_WORD cycle_state_copy_dst_addr_
;
101 size_t cycle_state_copy_words_
;
103 int (*render
)(void *, struct pixel_
*, size_t, size_t);
108 long long power_on_cycles_(void) {
113 gettimeofday(&tv
, NULL
);
114 r
+= LEM1802_POWER_ON_CYCLES
- (LEM1802_POWER_ON_CYCLES
/ 10);
115 r
+= tv
.tv_usec
% (LEM1802_POWER_ON_CYCLES
/ 5);
122 void pixel_color_(struct pixel_
*pix
, DCPU16_WORD color
) {
125 x
= (color
>> 0) & 0x000f;
126 pix
->r
= x
| (x
<< 4);
128 x
= (color
>> 4) & 0x000f;
129 pix
->g
= x
| (x
<< 4);
131 x
= (color
>> 8) & 0x000f;
132 pix
->b
= x
| (x
<< 4);
134 x
= (color
>> 12) & 0x000f;
135 pix
->a
= x
| (x
<< 4);
139 void pixbuf_border_paint_(struct pixel_
*pixbuf
, struct pixel_
*border
) {
142 TRACE("%s>> painted border", __func__
);
145 for (y
= 0; y
< PIX_BORDER
; y
++) {
146 for (x
= 0; x
< PIX_X
; x
++) {
149 i
= ((PIX_Y
- y
) * PIX_X
) + x
;
155 for (y
= PIX_BORDER
; y
< (PIX_Y
- PIX_BORDER
+ 1); y
++)
156 for (x
= 0; x
< PIX_BORDER
; x
++) {
159 pixbuf
[i
+ (PIX_X
- PIX_BORDER
)] = *border
;
164 void font_tile_paint_(struct pixel_
*p
, struct pixel_
*fg
, struct pixel_
*bg
, DCPU16_WORD
*tile
) {
166 unsigned char *font_bitmap
= (unsigned char *)tile
;
169 TRACE("%s>> fg:(%u,%u,%u) bg:(%u,%u,%u) font_bitmap:%02x %02x %02x %02x", __func__
,
172 font_bitmap
[0], font_bitmap
[1], font_bitmap
[2], font_bitmap
[3]);
175 for (pix_x
= 0; pix_x
< CELL_X_SZ
; pix_x
++) {
176 for (pix_y
= 0; pix_y
< CELL_Y_SZ
; pix_y
++) {
177 if ((font_bitmap
[pix_x
] >> pix_y
) & 0x01)
178 p
[((CELL_Y_SZ
- pix_y
- 1) * PIX_X
) + pix_x
] = *fg
;
180 p
[((CELL_Y_SZ
- pix_y
- 1) * PIX_X
) + pix_x
] = *bg
;
186 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
) {
187 struct pixel_
*tilestart
= pixbuf
; /* start of display */
188 unsigned int cell_x
= addr
% (PIX_X
/ CELL_X_SZ
),
189 cell_y
= addr
/ (PIX_X
/ CELL_X_SZ
);
190 struct pixel_ fg
, bg
;
191 DCPU16_WORD
*font_bitmap
;
194 cell_x
= (addr
- base
) % CELL_X
;
195 cell_y
= (addr
- base
) / CELL_X
;
198 TRACE("%s>> addr:0x%04x col:%u row:%u v:%hu",
201 cell_x
, cell_y
, mem
[addr
]);
204 blink
= mem
[addr
] & 0x0080;
206 /* tiles take two words each */
207 font_bitmap
= tiles
+ (2 * (mem
[addr
] & 0x7f));
209 pixel_color_(&bg
, palette
[(mem
[addr
] >> 8) & 0x0f]);
210 if (blink
&& blink_state
)
211 pixel_color_(&fg
, palette
[(mem
[addr
] >> 8) & 0x0f]);
213 pixel_color_(&fg
, palette
[(mem
[addr
] >> 12) & 0x0f]);
215 tilestart
+= (PIX_X
* PIX_BORDER
); /* skip top border */
216 tilestart
+= (CELL_Y_SZ
* PIX_X
) * cell_y
; /* skip down to row */
218 tilestart
+= PIX_BORDER
; /* skip side border */
219 tilestart
+= (CELL_X_SZ
) * cell_x
; /* skip to column */
221 font_tile_paint_(tilestart
, &fg
, &bg
, font_bitmap
);
225 void lem1802_pixbuf_refresh_full_(struct lem1802_
*display
, DCPU16_WORD
*mem
) {
226 struct pixel_ border
;
230 TRACE("%s>> video_base:0x%04x", __func__
, display
->video_base
);
233 if (display
->cycles_until_active_
) {
234 /* show cute power-up sequence.. */
235 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
239 if (display
->video_base
== 0) {
240 /* disconnected, blank display */
241 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
245 pixel_color_(&border
, display
->border_color
);
246 pixbuf_border_paint_(display
->pixbuf
, &border
);
248 for (tile
= 0; tile
< CELL_X
* CELL_Y
; tile
++) {
249 pixbuf_addr_paint_(display
->pixbuf
,
252 display
->video_base
+ tile
,
253 display
->palette_base
? mem
+ display
->palette_base
: (DCPU16_WORD
*)palette_default_
,
254 display
->font_base
? mem
+ display
->font_base
: (DCPU16_WORD
*)chargen_4x8_glyphs
,
255 display
->blink_state
);
260 int pixbuf_render_pnm_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
261 FILE *f
= (FILE *)data
;
267 for (i
= 0; i
< x
* y
; i
++) {
268 fwrite(&pixbuf
[i
].r
, 1, 1, f
);
269 fwrite(&pixbuf
[i
].g
, 1, 1, f
);
270 fwrite(&pixbuf
[i
].b
, 1, 1, f
);
279 int pixbuf_render_png_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
280 FILE *f
= (FILE *)data
;
286 png
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
291 info
= png_create_info_struct(png
);
293 png_destroy_write_struct(&png
, (png_infopp
)NULL
);
297 if (setjmp(png_jmpbuf(png
))) {
298 png_destroy_write_struct(&png
, &info
);
303 png_set_IHDR(png
, info
, x
, y
, 8, PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
304 png_set_invert_alpha(png
);
305 png_write_info(png
, info
);
306 for (i
= 0; i
< y
; i
++) {
307 png_write_row(png
, (unsigned char *)(pixbuf
+ (i
* x
)));
309 png_write_end(png
, info
);
311 png_destroy_write_struct(&png
, &info
);
318 #endif /* HAVE_LIBPNG */
320 #ifdef HAVE_LIBVNCSERVER
321 /* create and return a new screen, easiest to do here because we know the screen dimensions */
322 rfbScreenInfoPtr
lem1802_rfb_new(int argc
, char *argv
[]) {
324 int paddedWidth
= PIX_X
+ ( (PIX_X
& 3) ? (4 - (PIX_X
& 3)) : 0 );
326 int bitsPerSample
= 8;
327 int samplesPerPixel
= 3;
328 int bytesPerPixel
= 4;
330 s
= rfbGetScreen(&argc
, argv
, paddedWidth
, height
, bitsPerSample
, samplesPerPixel
, bytesPerPixel
);
334 s
->alwaysShared
= TRUE
;
337 s
->httpDir
= "../classes";
340 TRACE("%s>> s:%p", __func__
, VOIDP(s
));
341 TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__
, VOIDFP(s
->kbdAddEvent
), s
->frameBuffer
);
347 /* notify rfb server that pixels may have changed */
349 int pixbuf_render_vnc_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
350 rfbScreenInfoPtr s
= (rfbScreenInfoPtr
)data
;
357 rfbMarkRectAsModified(s
, 0, 0, x
, y
);
359 TRACE("%s>>", __func__
);
363 #endif /* HAVE_LIBVNCSERVER */
367 void lem1802_reset_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
368 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
372 display
->cycle_activated
= 0;
373 display
->video_base
= 0;
374 display
->font_base
= 0;
375 display
->palette_base
= 0;
376 display
->border_color
= 0;
378 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
380 display
->refresh_tally_
= 0;
381 display
->blink_tally_
= 0;
382 display
->blink_state
= 0;
384 display
->cycle_state_
= 0;
387 vm
->trace_cb_("%s>>", __func__
);
392 void lem1802_cycle_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
393 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
397 for more cycle-accuracy, could step through video memory, if set,
399 for now just count cycles and issue a full refresh/render
403 display
->blink_tally_
++;
404 if (display
->blink_tally_
>= display
->blink_rate
) {
405 display
->blink_tally_
= 0;
406 display
->blink_state
^= 1;
407 TRACE("%s>> blink:%u (%u cycles)", __func__
, display
->blink_state
, display
->blink_rate
);
410 display
->refresh_tally_
++;
411 if (display
->refresh_tally_
>= display
->refresh_rate
) {
412 display
->refresh_tally_
= 0;
414 TRACE("%s>> refresh", __func__
);
415 lem1802_pixbuf_refresh_full_(display
, vm
->ram
);
416 display
->render(display
->renderer_data
, display
->pixbuf
, PIX_X
, PIX_Y
);
419 switch (display
->cycle_state_
) {
423 case CYCLE_COPY_TO_RAM
:
424 TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x",
426 display
->cycle_state_copy_words_
,
427 VOIDP(display
->cycle_state_copy_src_ptr_
),
428 display
->cycle_state_copy_dst_addr_
);
429 vm
->ram
[display
->cycle_state_copy_dst_addr_
] = *display
->cycle_state_copy_src_ptr_
;
430 display
->cycle_state_copy_dst_addr_
++;
431 display
->cycle_state_copy_src_ptr_
++;
432 display
->cycle_state_copy_words_
--;
433 if (display
->cycle_state_copy_words_
== 0) {
434 display
->cycle_state_
= CYCLE_IDLE
;
439 if (display
->cycles_until_active_
) {
440 display
->cycles_until_active_
--;
441 if (display
->cycles_until_active_
== 0) {
442 TRACE("%s>> display now active", __func__
);
448 void lem1802_hwi_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
449 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
450 DCPU16_WORD reg_a
= vm
->reg
[DCPU16_REG_A
];
451 DCPU16_WORD reg_b
= vm
->reg
[DCPU16_REG_B
];
453 TRACE("%s>> A:0x%04x B:0x%04x", __func__
, reg_a
, reg_b
);
456 case 0: /* MEM_MAP_SCREEN */
457 if (display
->cycle_activated
== 0 && reg_b
) {
458 display
->cycle_activated
= vm
->cycle_
;
459 display
->cycles_until_active_
= power_on_cycles_();
461 display
->video_base
= reg_b
;
463 display
->cycle_activated
= 0;
466 case 1: /* MEM_MAP_FONT */
467 display
->font_base
= reg_b
;
470 case 2: /* MEM_MAP_PALETTE */
471 display
->palette_base
= reg_b
;
474 case 3: /* SET_BORDER_COLOR */
475 display
->border_color
= reg_b
& 0x000f;
478 case 4: /* MEM_DUMP_FONT */
479 display
->cycle_state_copy_src_ptr_
= (DCPU16_WORD
*)chargen_4x8_glyphs
;
480 display
->cycle_state_copy_dst_addr_
= reg_b
;
481 display
->cycle_state_copy_words_
= 256;
482 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
483 dcpu16_cycle_inc(vm
, 256);
486 case 5: /* MEM_DUMP_PALETTE */
487 display
->cycle_state_copy_src_ptr_
= palette_default_
;
488 display
->cycle_state_copy_dst_addr_
= reg_b
;
489 display
->cycle_state_copy_words_
= 16;
490 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
491 dcpu16_cycle_inc(vm
, 16);
496 static struct renderer_
{
499 int (*renderer
)(void *, struct pixel_
*, size_t, size_t);
500 } lem1802_renderers_
[] = {
501 { "pnm", "filename", pixbuf_render_pnm_
},
503 { "png", "filename", pixbuf_render_png_
},
504 #endif /* HAVE_LIBPNG */
505 #ifdef HAVE_LIBVNCSERVER
506 { "vnc", "", pixbuf_render_vnc_
},
507 #endif /* HAVE_LIBVNCSERVER */
508 { "none", "", NULL
},
513 char *lem1802_renderers_iter(void **iterp
, char **name
, char **args
) {
514 struct renderer_
**r
= (struct renderer_
**)iterp
;
517 *r
= lem1802_renderers_
;
521 if ((*r
)->name
== NULL
) {
532 int lem1802_data_init_(struct dcpu16_hw
*hw
, void *data
) {
535 hw
->data
= calloc(1, sizeof(struct lem1802_
));
536 if (hw
->data
== NULL
) {
537 hw
->vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
541 ((struct lem1802_
*)(hw
->data
))->pixbuf
= calloc(PIX_X
* PIX_Y
, sizeof *((struct lem1802_
*)(hw
->data
))->pixbuf
);
542 if (((struct lem1802_
*)(hw
->data
))->pixbuf
== NULL
) {
543 hw
->vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
549 ((struct lem1802_
*)(hw
->data
))->refresh_rate
= 1666;
550 ((struct lem1802_
*)(hw
->data
))->blink_rate
= 75000;
555 void lem1802_data_free_(struct dcpu16_hw
*hw
) {
558 if (((struct lem1802_
*)(hw
->data
))->pixbuf
) {
559 free(((struct lem1802_
*)(hw
->data
))->pixbuf
);
560 ((struct lem1802_
*)(hw
->data
))->pixbuf
= NULL
;
568 static struct dcpu16_hw_ctl_cmd ctl_
[] = {
569 { "blink_rate", "const unsigned int *rate", "unsigned int *rate", "sets or gets cycles per blink toggle" },
570 { "refresh_rate", "const unsigned int *rate", "unsigned int *rate", "sets or gets cycles per screen refresh" },
571 #ifdef HAVE_LIBVNCSERVER
572 { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this lem1802 instance with an rfb display" },
573 #endif /* HAVE_LIBVNCSERVER */
574 { "renderer", "const char *", "NULL", "sets this lem1802 instance to use renderer" },
575 { "renderer_data", "void *", "NULL", "sets renderer-specific data" },
576 { NULL
, NULL
, NULL
, NULL
}
578 int lem1802_data_ctl_(struct dcpu16_hw
*hw
, const char *cmd
, void *data_in
, void *data_out
) {
579 if (strcmp(cmd
, "blink_rate") == 0) {
580 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
581 const unsigned int *rate_in
= (const unsigned int *)data_in
;
582 unsigned int *rate_out
= (unsigned int *)data_out
;
585 *rate_out
= display
->blink_rate
;
589 display
->blink_rate
= *rate_in
;
595 if (strcmp(cmd
, "refresh_rate") == 0) {
596 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
597 const unsigned int *rate_in
= (const unsigned int *)data_in
;
598 unsigned int *rate_out
= (unsigned int *)data_out
;
601 *rate_out
= display
->refresh_rate
;
605 display
->refresh_rate
= *rate_in
;
611 #ifdef HAVE_LIBVNCSERVER
612 if (strcmp(cmd
, "associate_rfbScreen") == 0) {
613 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
614 rfbScreenInfoPtr rfbScreen
= (rfbScreenInfoPtr
)data_out
;
617 if (rfbScreen
== NULL
)
620 rfbScreen
->desktopName
= "NYA ELEKTRISKA LEM1802";
621 rfbScreen
->frameBuffer
= (char *)display
->pixbuf
;
625 #endif /* HAVE_LIBVNCSERVER */
627 if (strcmp(cmd
, "renderer") == 0) {
628 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
629 char *renderer
= (char *)data_in
;
633 for (r
= lem1802_renderers_
; r
->renderer
; r
++) {
634 if (strcmp(renderer
, r
->name
) == 0) {
635 display
->render
= r
->renderer
;
636 TRACE("%s>> renderer set to %s", __func__
, renderer
);
641 hw
->vm
->warn_cb_("unknown renderer '%s'", renderer
);
646 if (strcmp(cmd
, "renderer_data") == 0) {
647 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
650 display
->renderer_data
= data_in
;
659 static struct dcpu16_hw hw_
= {
660 .name_
= "LEM1802 - Low Energy Monitor",
667 .cycle
= lem1802_cycle_
,
668 .reset
= lem1802_reset_
,
669 .data
= (struct lem1802_
*)NULL
672 struct dcpu16_hw_module dcpu16_hw_module_lem1802
= {
674 .data_init
= lem1802_data_init_
,
675 .data_free
= lem1802_data_free_
,
676 .ctl
= lem1802_data_ctl_
,