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 */
323 rfbScreenInfoPtr
lem1802_rfb_new_(int argc
, char *argv
[]) {
325 int paddedWidth
= PIX_X
+ ( (PIX_X
& 3) ? (4 - (PIX_X
& 3)) : 0 );
327 int bitsPerSample
= 8;
328 int samplesPerPixel
= 3;
329 int bytesPerPixel
= 4;
331 s
= rfbGetScreen(&argc
, argv
, paddedWidth
, height
, bitsPerSample
, samplesPerPixel
, bytesPerPixel
);
335 s
->alwaysShared
= TRUE
;
338 s
->httpDir
= "../classes";
341 TRACE("%s>> s:%p", __func__
, VOIDP(s
));
342 TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__
, VOIDFP(s
->kbdAddEvent
), s
->frameBuffer
);
348 /* notify rfb server that pixels may have changed */
350 int pixbuf_render_vnc_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
351 rfbScreenInfoPtr s
= (rfbScreenInfoPtr
)data
;
358 rfbMarkRectAsModified(s
, 0, 0, x
, y
);
360 TRACE("%s>>", __func__
);
364 #endif /* HAVE_LIBVNCSERVER */
368 void lem1802_reset_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
369 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
373 display
->cycle_activated
= 0;
374 display
->video_base
= 0;
375 display
->font_base
= 0;
376 display
->palette_base
= 0;
377 display
->border_color
= 0;
379 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
381 display
->refresh_tally_
= 0;
382 display
->blink_tally_
= 0;
383 display
->blink_state
= 0;
385 display
->cycle_state_
= 0;
388 vm
->trace_cb_("%s>>", __func__
);
393 void lem1802_cycle_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
394 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
398 for more cycle-accuracy, could step through video memory, if set,
400 for now just count cycles and issue a full refresh/render
404 display
->blink_tally_
++;
405 if (display
->blink_tally_
>= display
->blink_rate
) {
406 display
->blink_tally_
= 0;
407 display
->blink_state
^= 1;
408 TRACE("%s>> blink:%u (%u cycles)", __func__
, display
->blink_state
, display
->blink_rate
);
411 display
->refresh_tally_
++;
412 if (display
->refresh_tally_
>= display
->refresh_rate
) {
413 display
->refresh_tally_
= 0;
415 TRACE("%s>> refresh", __func__
);
416 lem1802_pixbuf_refresh_full_(display
, vm
->ram
);
417 display
->render(display
->renderer_data
, display
->pixbuf
, PIX_X
, PIX_Y
);
420 switch (display
->cycle_state_
) {
424 case CYCLE_COPY_TO_RAM
:
425 TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x",
427 display
->cycle_state_copy_words_
,
428 VOIDP(display
->cycle_state_copy_src_ptr_
),
429 display
->cycle_state_copy_dst_addr_
);
430 vm
->ram
[display
->cycle_state_copy_dst_addr_
] = *display
->cycle_state_copy_src_ptr_
;
431 display
->cycle_state_copy_dst_addr_
++;
432 display
->cycle_state_copy_src_ptr_
++;
433 display
->cycle_state_copy_words_
--;
434 if (display
->cycle_state_copy_words_
== 0) {
435 display
->cycle_state_
= CYCLE_IDLE
;
440 if (display
->cycles_until_active_
) {
441 display
->cycles_until_active_
--;
442 if (display
->cycles_until_active_
== 0) {
443 TRACE("%s>> display now active", __func__
);
449 void lem1802_hwi_(struct dcpu16
*vm
, struct dcpu16_hw
*hw
) {
450 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
451 DCPU16_WORD reg_a
= vm
->reg
[DCPU16_REG_A
];
452 DCPU16_WORD reg_b
= vm
->reg
[DCPU16_REG_B
];
454 TRACE("%s>> A:0x%04x B:0x%04x", __func__
, reg_a
, reg_b
);
457 case 0: /* MEM_MAP_SCREEN */
458 if (display
->cycle_activated
== 0 && reg_b
) {
459 display
->cycle_activated
= vm
->cycle_
;
460 display
->cycles_until_active_
= power_on_cycles_();
462 display
->video_base
= reg_b
;
464 display
->cycle_activated
= 0;
467 case 1: /* MEM_MAP_FONT */
468 display
->font_base
= reg_b
;
471 case 2: /* MEM_MAP_PALETTE */
472 display
->palette_base
= reg_b
;
475 case 3: /* SET_BORDER_COLOR */
476 display
->border_color
= reg_b
& 0x000f;
479 case 4: /* MEM_DUMP_FONT */
480 display
->cycle_state_copy_src_ptr_
= (DCPU16_WORD
*)chargen_4x8_glyphs
;
481 display
->cycle_state_copy_dst_addr_
= reg_b
;
482 display
->cycle_state_copy_words_
= 256;
483 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
484 dcpu16_cycle_inc(vm
, 256);
487 case 5: /* MEM_DUMP_PALETTE */
488 display
->cycle_state_copy_src_ptr_
= palette_default_
;
489 display
->cycle_state_copy_dst_addr_
= reg_b
;
490 display
->cycle_state_copy_words_
= 16;
491 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
492 dcpu16_cycle_inc(vm
, 16);
497 static struct renderer_
{
500 int (*renderer
)(void *, struct pixel_
*, size_t, size_t);
501 } lem1802_renderers_
[] = {
502 { "pnm", "filename", pixbuf_render_pnm_
},
504 { "png", "filename", pixbuf_render_png_
},
505 #endif /* HAVE_LIBPNG */
506 #ifdef HAVE_LIBVNCSERVER
507 { "vnc", "", pixbuf_render_vnc_
},
508 #endif /* HAVE_LIBVNCSERVER */
509 { "none", "", NULL
},
514 char *lem1802_renderers_iter_(void **iterp
, char **name
, char **args
) {
515 struct renderer_
**r
= (struct renderer_
**)iterp
;
518 *r
= lem1802_renderers_
;
522 if ((*r
)->name
== NULL
) {
534 int lem1802_data_init_(struct dcpu16_hw
*hw
, void *data
) {
537 hw
->data
= calloc(1, sizeof(struct lem1802_
));
538 if (hw
->data
== NULL
) {
539 hw
->vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
543 ((struct lem1802_
*)(hw
->data
))->pixbuf
= calloc(PIX_X
* PIX_Y
, sizeof *((struct lem1802_
*)(hw
->data
))->pixbuf
);
544 if (((struct lem1802_
*)(hw
->data
))->pixbuf
== NULL
) {
545 hw
->vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
551 ((struct lem1802_
*)(hw
->data
))->refresh_rate
= 1666;
552 ((struct lem1802_
*)(hw
->data
))->blink_rate
= 75000;
558 void lem1802_data_free_(struct dcpu16_hw
*hw
) {
561 if (((struct lem1802_
*)(hw
->data
))->pixbuf
) {
562 free(((struct lem1802_
*)(hw
->data
))->pixbuf
);
563 ((struct lem1802_
*)(hw
->data
))->pixbuf
= NULL
;
571 static struct dcpu16_hw_ctl_cmd ctl_
[] = {
572 { "blink_rate", "const unsigned int *", "unsigned int *", "sets or gets cycles per blink toggle" },
573 { "refresh_rate", "const unsigned int *", "unsigned int *", "sets or gets cycles per screen refresh" },
574 #ifdef HAVE_LIBVNCSERVER
575 { "new_rfbScreen", "struct { int argc; char **argv;} *", "rfbScreenInfoPtr *", "allocates a new rfb screen" },
576 { "associate_rfbScreen", "rfbScreenInfoPtr", "NULL", "associates this lem1802 instance with an rfb display" },
577 #endif /* HAVE_LIBVNCSERVER */
578 { "renderers_iter", "void **", "struct {char *name; char *args;} *", "returns the next renderer this module is capable of using" },
579 { "renderer", "const char *", "NULL", "sets this lem1802 instance to use renderer" },
580 { "renderer_data", "void *", "NULL", "sets renderer-specific data" },
581 { NULL
, NULL
, NULL
, NULL
}
585 int lem1802_data_ctl_(struct dcpu16_hw
*hw
, const char *cmd
, void *data_in
, void *data_out
) {
586 if (strcmp(cmd
, "blink_rate") == 0) {
587 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
588 const unsigned int *rate_in
= (const unsigned int *)data_in
;
589 unsigned int *rate_out
= (unsigned int *)data_out
;
592 *rate_out
= display
->blink_rate
;
596 display
->blink_rate
= *rate_in
;
602 if (strcmp(cmd
, "refresh_rate") == 0) {
603 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
604 const unsigned int *rate_in
= (const unsigned int *)data_in
;
605 unsigned int *rate_out
= (unsigned int *)data_out
;
608 *rate_out
= display
->refresh_rate
;
612 display
->refresh_rate
= *rate_in
;
618 #ifdef HAVE_LIBVNCSERVER
619 if (strcmp(cmd
, "new_rfbScreen") == 0) {
620 struct args_
{ int argc
; char **argv
;} *in
= (struct args_
*)data_in
;
621 rfbScreenInfoPtr
*s_out
= (rfbScreenInfoPtr
*)data_out
;
623 if (in
== NULL
|| s_out
== NULL
)
626 *s_out
= lem1802_rfb_new_(in
->argc
, in
->argv
);
631 if (strcmp(cmd
, "associate_rfbScreen") == 0) {
632 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
633 rfbScreenInfoPtr rfbScreen
= (rfbScreenInfoPtr
)data_in
;
636 if (rfbScreen
== NULL
)
639 rfbScreen
->desktopName
= "NYA ELEKTRISKA LEM1802";
640 rfbScreen
->frameBuffer
= (char *)display
->pixbuf
;
644 #endif /* HAVE_LIBVNCSERVER */
646 if (strcmp(cmd
, "renderers_iter") == 0) {
647 void **iterp
= (void **)data_in
;
651 } *parg
= (struct packed_out_
*)data_out
;
653 if (iterp
== NULL
|| parg
== NULL
)
656 (void)lem1802_renderers_iter_(iterp
, &parg
->name
, &parg
->args
);
661 if (strcmp(cmd
, "renderer") == 0) {
662 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
663 char *renderer
= (char *)data_in
;
667 for (r
= lem1802_renderers_
; r
->renderer
; r
++) {
668 if (strcmp(renderer
, r
->name
) == 0) {
669 display
->render
= r
->renderer
;
670 TRACE("%s>> renderer set to %s", __func__
, renderer
);
675 hw
->vm
->warn_cb_("unknown renderer '%s'", renderer
);
680 if (strcmp(cmd
, "renderer_data") == 0) {
681 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
684 display
->renderer_data
= data_in
;
693 static struct dcpu16_hw hw_
= {
694 .name_
= "LEM1802 - Low Energy Monitor",
701 .cycle
= lem1802_cycle_
,
702 .reset
= lem1802_reset_
,
703 .data
= (struct lem1802_
*)NULL
706 struct dcpu16_hw_module dcpu16_hw_module_lem1802
= {
708 .data_init
= lem1802_data_init_
,
709 .data_free
= lem1802_data_free_
,
710 .ctl
= lem1802_data_ctl_
,