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 static dcpu16_hw_signal_t lem1802_reset_
;
36 static dcpu16_hw_signal_t lem1802_cycle_
;
37 static dcpu16_hw_signal_t lem1802_hwi_
;
38 static struct dcpu16_hw hw_
= {
39 .name_
= "LEM1802 - Low Energy Monitor",
46 .cycle
= lem1802_cycle_
,
47 .reset
= lem1802_reset_
,
48 .data
= (struct lem1802_
*)NULL
51 #define LEM1802_POWER_ON_CYCLES 100000 /* this should vary by, let us say, 10% */
53 #define PIX_X 160 /* pixels in display */
54 #define PIX_Y 128 /* pixels in display */
55 #define PIX_BORDER 16 /* border pixels from edge to first tile */
58 #define CELL_X ((PIX_X - (2 * PIX_BORDER)) / CELL_X_SZ) /* tiles in display */
59 #define CELL_Y ((PIX_Y - (2 * PIX_BORDER)) / CELL_Y_SZ) /* tiles in display */
61 #define PALETTE_ENTRIES 16
62 static const DCPU16_WORD palette_default_
[PALETTE_ENTRIES
] = {
70 0x0aaf, /* pale blue */
72 0x055f, /* light blue */
73 0x05f5, /* light green*/
74 0x05ff, /* light cyan */
75 0x0f55, /* light red */
76 0x0f5f, /* light magenta */
77 0x0ff5, /* light yellow */
89 long long cycle_activated
; /* running since */
90 long long cycles_until_active_
; /* for tracking power-up delay */
92 DCPU16_WORD video_base
;
93 DCPU16_WORD font_base
;
94 DCPU16_WORD palette_base
;
95 DCPU16_WORD border_color
;
96 struct pixel_
*pixbuf
;
98 unsigned int refresh_rate
; /* redraw every n cycles */
99 unsigned int refresh_tally_
; /* tick */
101 unsigned int blink_rate
; /* toggle every n cycles? still figuring this out.. */
102 unsigned int blink_tally_
; /* tick */
103 unsigned int blink_state
;
109 const DCPU16_WORD
*cycle_state_copy_src_ptr_
;
110 DCPU16_WORD cycle_state_copy_dst_addr_
;
111 size_t cycle_state_copy_words_
;
113 int (*render
)(void *, struct pixel_
*, size_t, size_t);
118 long long power_on_cycles_(void) {
123 gettimeofday(&tv
, NULL
);
124 r
+= LEM1802_POWER_ON_CYCLES
- (LEM1802_POWER_ON_CYCLES
/ 10);
125 r
+= tv
.tv_usec
% (LEM1802_POWER_ON_CYCLES
/ 5);
132 void pixel_color_(struct pixel_
*pix
, DCPU16_WORD color
) {
135 x
= (color
>> 0) & 0x000f;
136 pix
->r
= x
| (x
<< 4);
138 x
= (color
>> 4) & 0x000f;
139 pix
->g
= x
| (x
<< 4);
141 x
= (color
>> 8) & 0x000f;
142 pix
->b
= x
| (x
<< 4);
144 x
= (color
>> 12) & 0x000f;
145 pix
->a
= x
| (x
<< 4);
149 void pixbuf_border_paint_(struct pixel_
*pixbuf
, struct pixel_
*border
) {
152 TRACE("%s>> painted border", __func__
);
155 for (y
= 0; y
< PIX_BORDER
; y
++) {
156 for (x
= 0; x
< PIX_X
; x
++) {
159 i
= ((PIX_Y
- y
) * PIX_X
) + x
;
165 for (y
= PIX_BORDER
; y
< (PIX_Y
- PIX_BORDER
+ 1); y
++)
166 for (x
= 0; x
< PIX_BORDER
; x
++) {
169 pixbuf
[i
+ (PIX_X
- PIX_BORDER
)] = *border
;
174 void font_tile_paint_(struct pixel_
*p
, struct pixel_
*fg
, struct pixel_
*bg
, DCPU16_WORD
*tile
) {
176 unsigned char *font_bitmap
= (unsigned char *)tile
;
179 TRACE("%s>> fg:(%u,%u,%u) bg:(%u,%u,%u) font_bitmap:%02x %02x %02x %02x", __func__
,
182 font_bitmap
[0], font_bitmap
[1], font_bitmap
[2], font_bitmap
[3]);
185 for (pix_x
= 0; pix_x
< CELL_X_SZ
; pix_x
++) {
186 for (pix_y
= 0; pix_y
< CELL_Y_SZ
; pix_y
++) {
187 if ((font_bitmap
[pix_x
] >> pix_y
) & 0x01)
188 p
[((CELL_Y_SZ
- pix_y
- 1) * PIX_X
) + pix_x
] = *fg
;
190 p
[((CELL_Y_SZ
- pix_y
- 1) * PIX_X
) + pix_x
] = *bg
;
196 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
) {
197 struct pixel_
*tilestart
= pixbuf
; /* start of display */
198 unsigned int cell_x
= addr
% (PIX_X
/ CELL_X_SZ
),
199 cell_y
= addr
/ (PIX_X
/ CELL_X_SZ
);
200 struct pixel_ fg
, bg
;
201 DCPU16_WORD
*font_bitmap
;
204 cell_x
= (addr
- base
) % CELL_X
;
205 cell_y
= (addr
- base
) / CELL_X
;
208 TRACE("%s>> addr:0x%04x col:%u row:%u v:%hu",
211 cell_x
, cell_y
, mem
[addr
]);
214 blink
= mem
[addr
] & 0x0080;
216 /* tiles take two words each */
217 font_bitmap
= tiles
+ (2 * (mem
[addr
] & 0x7f));
219 pixel_color_(&bg
, palette
[(mem
[addr
] >> 8) & 0x0f]);
220 if (blink
&& blink_state
)
221 pixel_color_(&fg
, palette
[(mem
[addr
] >> 8) & 0x0f]);
223 pixel_color_(&fg
, palette
[(mem
[addr
] >> 12) & 0x0f]);
225 tilestart
+= (PIX_X
* PIX_BORDER
); /* skip top border */
226 tilestart
+= (CELL_Y_SZ
* PIX_X
) * cell_y
; /* skip down to row */
228 tilestart
+= PIX_BORDER
; /* skip side border */
229 tilestart
+= (CELL_X_SZ
) * cell_x
; /* skip to column */
231 font_tile_paint_(tilestart
, &fg
, &bg
, font_bitmap
);
235 void lem1802_pixbuf_refresh_full_(struct lem1802_
*display
, DCPU16_WORD
*mem
) {
236 struct pixel_ border
;
240 TRACE("%s>> video_base:0x%04x", __func__
, display
->video_base
);
243 if (display
->cycles_until_active_
) {
244 /* show cute power-up sequence.. */
245 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
249 if (display
->video_base
== 0) {
250 /* disconnected, blank display */
251 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
255 pixel_color_(&border
, display
->border_color
);
256 pixbuf_border_paint_(display
->pixbuf
, &border
);
258 for (tile
= 0; tile
< CELL_X
* CELL_Y
; tile
++) {
259 pixbuf_addr_paint_(display
->pixbuf
,
262 display
->video_base
+ tile
,
263 display
->palette_base
? mem
+ display
->palette_base
: (DCPU16_WORD
*)palette_default_
,
264 display
->font_base
? mem
+ display
->font_base
: (DCPU16_WORD
*)chargen_4x8_glyphs
,
265 display
->blink_state
);
270 int pixbuf_render_pnm_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
271 FILE *f
= (FILE *)data
;
277 for (i
= 0; i
< x
* y
; i
++) {
278 fwrite(&pixbuf
[i
].r
, 1, 1, f
);
279 fwrite(&pixbuf
[i
].g
, 1, 1, f
);
280 fwrite(&pixbuf
[i
].b
, 1, 1, f
);
289 int pixbuf_render_png_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
290 FILE *f
= (FILE *)data
;
296 png
= png_create_write_struct(PNG_LIBPNG_VER_STRING
, NULL
, NULL
, NULL
);
301 info
= png_create_info_struct(png
);
303 png_destroy_write_struct(&png
, (png_infopp
)NULL
);
307 if (setjmp(png_jmpbuf(png
))) {
308 png_destroy_write_struct(&png
, &info
);
313 png_set_IHDR(png
, info
, x
, y
, 8, PNG_COLOR_TYPE_RGB_ALPHA
, PNG_INTERLACE_NONE
, PNG_COMPRESSION_TYPE_DEFAULT
, PNG_FILTER_TYPE_DEFAULT
);
314 png_set_invert_alpha(png
);
315 png_write_info(png
, info
);
316 for (i
= 0; i
< y
; i
++) {
317 png_write_row(png
, (unsigned char *)(pixbuf
+ (i
* x
)));
319 png_write_end(png
, info
);
321 png_destroy_write_struct(&png
, &info
);
328 #endif /* HAVE_LIBPNG */
330 #ifdef HAVE_LIBVNCSERVER
331 /* create and return a new screen, easiest to do here because we know the screen dimensions */
332 rfbScreenInfoPtr
lem1802_rfb_new(int argc
, char *argv
[]) {
334 int paddedWidth
= PIX_X
+ ( (PIX_X
& 3) ? (4 - (PIX_X
& 3)) : 0 );
336 int bitsPerSample
= 8;
337 int samplesPerPixel
= 3;
338 int bytesPerPixel
= 4;
340 s
= rfbGetScreen(&argc
, argv
, paddedWidth
, height
, bitsPerSample
, samplesPerPixel
, bytesPerPixel
);
344 s
->alwaysShared
= TRUE
;
347 s
->httpDir
= "../classes";
350 TRACE("%s>> s:%p", __func__
, VOIDP(s
));
351 TRACE("%s>> s->kbdAddEvent:%p s->frameBuffer:%p", __func__
, VOIDFP(s
->kbdAddEvent
), s
->frameBuffer
);
356 /* set up a new screen to see our pixels */
357 void lem1802_vnc_associate(struct dcpu16_hw
*hw
, rfbScreenInfoPtr s
) {
358 struct lem1802_
*display
= (struct lem1802_
*)hw
->data
;
360 s
->desktopName
= "NYA ELEKTRISKA LEM1802";
361 s
->frameBuffer
= (char *)display
->pixbuf
;
363 TRACE("%s>> s:%p\n", __func__
, VOIDP(s
));
366 /* notify rfb server that pixels may have changed */
368 int pixbuf_render_vnc_(void *data
, struct pixel_
*pixbuf
, size_t x
, size_t y
) {
369 rfbScreenInfoPtr s
= (rfbScreenInfoPtr
)data
;
376 rfbMarkRectAsModified(s
, 0, 0, x
, y
);
378 TRACE("%s>>", __func__
);
382 #endif /* HAVE_LIBVNCSERVER */
386 void lem1802_reset_(struct dcpu16
*vm
, void *data
) {
387 struct lem1802_
*display
= (struct lem1802_
*)data
;
391 display
->cycle_activated
= 0;
392 display
->video_base
= 0;
393 display
->font_base
= 0;
394 display
->palette_base
= 0;
395 display
->border_color
= 0;
397 memset(display
->pixbuf
, 0, PIX_X
* PIX_Y
* sizeof *display
->pixbuf
);
399 display
->refresh_tally_
= 0;
400 display
->blink_tally_
= 0;
401 display
->blink_state
= 0;
403 display
->cycle_state_
= 0;
406 vm
->trace_cb_("%s>>", __func__
);
411 void lem1802_cycle_(struct dcpu16
*vm
, void *data
) {
412 struct lem1802_
*display
= (struct lem1802_
*)data
;
416 for more cycle-accuracy, could step through video memory, if set,
418 for now just count cycles and issue a full refresh/render
422 display
->blink_tally_
++;
423 if (display
->blink_tally_
>= display
->blink_rate
) {
424 display
->blink_tally_
= 0;
425 display
->blink_state
^= 1;
426 TRACE("%s>> blink:%u (%u cycles)", __func__
, display
->blink_state
, display
->blink_rate
);
429 display
->refresh_tally_
++;
430 if (display
->refresh_tally_
>= display
->refresh_rate
) {
431 display
->refresh_tally_
= 0;
433 TRACE("%s>> refresh", __func__
);
434 lem1802_pixbuf_refresh_full_(display
, vm
->ram
);
435 display
->render(display
->renderer_data
, display
->pixbuf
, PIX_X
, PIX_Y
);
438 switch (display
->cycle_state_
) {
442 case CYCLE_COPY_TO_RAM
:
443 TRACE("%s>> copy_to_ram words:%zu src:%p dst_addr:0x%04x",
445 display
->cycle_state_copy_words_
,
446 VOIDP(display
->cycle_state_copy_src_ptr_
),
447 display
->cycle_state_copy_dst_addr_
);
448 vm
->ram
[display
->cycle_state_copy_dst_addr_
] = *display
->cycle_state_copy_src_ptr_
;
449 display
->cycle_state_copy_dst_addr_
++;
450 display
->cycle_state_copy_src_ptr_
++;
451 display
->cycle_state_copy_words_
--;
452 if (display
->cycle_state_copy_words_
== 0) {
453 display
->cycle_state_
= CYCLE_IDLE
;
458 if (display
->cycles_until_active_
) {
459 display
->cycles_until_active_
--;
460 if (display
->cycles_until_active_
== 0) {
461 TRACE("%s>> display now active", __func__
);
467 void lem1802_hwi_(struct dcpu16
*vm
, void *data
) {
468 struct lem1802_
*display
= (struct lem1802_
*)data
;
469 DCPU16_WORD reg_a
= vm
->reg
[DCPU16_REG_A
];
470 DCPU16_WORD reg_b
= vm
->reg
[DCPU16_REG_B
];
472 TRACE("%s>> A:0x%04x B:0x%04x", __func__
, reg_a
, reg_b
);
475 case 0: /* MEM_MAP_SCREEN */
476 if (display
->cycle_activated
== 0 && reg_b
) {
477 display
->cycle_activated
= vm
->cycle_
;
478 display
->cycles_until_active_
= power_on_cycles_();
480 display
->video_base
= reg_b
;
482 display
->cycle_activated
= 0;
485 case 1: /* MEM_MAP_FONT */
486 display
->font_base
= reg_b
;
489 case 2: /* MEM_MAP_PALETTE */
490 display
->palette_base
= reg_b
;
493 case 3: /* SET_BORDER_COLOR */
494 display
->border_color
= reg_b
& 0x000f;
497 case 4: /* MEM_DUMP_FONT */
498 display
->cycle_state_copy_src_ptr_
= (DCPU16_WORD
*)chargen_4x8_glyphs
;
499 display
->cycle_state_copy_dst_addr_
= reg_b
;
500 display
->cycle_state_copy_words_
= 256;
501 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
502 dcpu16_cycle_inc(vm
, 256);
505 case 5: /* MEM_DUMP_PALETTE */
506 display
->cycle_state_copy_src_ptr_
= palette_default_
;
507 display
->cycle_state_copy_dst_addr_
= reg_b
;
508 display
->cycle_state_copy_words_
= 16;
509 display
->cycle_state_
= CYCLE_COPY_TO_RAM
;
510 dcpu16_cycle_inc(vm
, 16);
515 static struct renderer_
{
518 int (*renderer
)(void *, struct pixel_
*, size_t, size_t);
519 } lem1802_renderers_
[] = {
520 { "pnm", "filename", pixbuf_render_pnm_
},
522 { "png", "filename", pixbuf_render_png_
},
523 #endif /* HAVE_LIBPNG */
524 #ifdef HAVE_LIBVNCSERVER
525 { "vnc", "", pixbuf_render_vnc_
},
526 #endif /* HAVE_LIBVNCSERVER */
527 { "none", "", NULL
},
531 int lem1802_renderer_set(struct dcpu16_hw
*hw
, const char *renderer
, void *data
) {
534 for (r
= lem1802_renderers_
; r
->renderer
; r
++) {
535 if (strcmp(renderer
, r
->name
) == 0) {
536 ((struct lem1802_
*)(hw
->data
))->render
= r
->renderer
;
537 ((struct lem1802_
*)(hw
->data
))->renderer_data
= data
;
538 TRACE("%s>> renderer set to %s", __func__
, renderer
);
546 char *lem1802_renderers_iter(void **iterp
, char **name
, char **args
) {
547 struct renderer_
**r
= (struct renderer_
**)iterp
;
550 *r
= lem1802_renderers_
;
554 if ((*r
)->name
== NULL
) {
565 /* instantitate a new display */
566 struct dcpu16_hw
*lem1802_new(struct dcpu16
*vm
) {
567 struct dcpu16_hw
*hw
;
569 hw
= calloc(1, sizeof *hw
);
571 vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
575 memcpy(hw
, &hw_
, sizeof *hw
);
577 hw
->data
= calloc(1, sizeof(struct lem1802_
));
578 if (hw
->data
== NULL
) {
579 vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
584 ((struct lem1802_
*)(hw
->data
))->pixbuf
= calloc(1, PIX_X
* PIX_Y
* sizeof *((struct lem1802_
*)(hw
->data
))->pixbuf
);
585 if (((struct lem1802_
*)(hw
->data
))->pixbuf
== NULL
) {
586 vm
->warn_cb_("%s():%s", "calloc", strerror(errno
));
592 ((struct lem1802_
*)(hw
->data
))->refresh_rate
= 1666;
593 ((struct lem1802_
*)(hw
->data
))->blink_rate
= 75000;
600 void lem1802_del(struct dcpu16_hw
**hw
) {
604 if (((struct lem1802_
*)(*hw
)->data
)->pixbuf
) {
605 free(((struct lem1802_
*)(*hw
)->data
)->pixbuf
);
606 ((struct lem1802_
*)(*hw
)->data
)->pixbuf
= NULL
;