/
pebble_snowy_display.c
2185 lines (1903 loc) · 117 KB
/
pebble_snowy_display.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* Copyright (c) 2014
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* 8-bit color display connected through SPI bus. The 8 bits are organized as (starting from MSB):
* 2 bits red, 2 bits green, 2 bits blue, 2 bits of 0.
*
* This display is used in the Pebble Snowy platform and actually represents an FPGA connected
* to a LPM012A220A display. The FPGA implements the SPI interface.
*
* Some example colors:
* black: 0x00
* white: 0xFC
* red: 0xC0
* green: 0x30
* blue: 0x0C
*
*
* This device implements various versions of the FPGA programming, identified by the
* PDisplayCmdSet enum:
*
* PDISPLAY_CMD_SET_0:
* -------------------
* This is used by the boot loader and has cmd IDs that tell the FPGA to display pre-canned
* images
*
*
* PDISPLAY_CMD_SET_1:
* -------------------
* The display expects complete frames to be setn through the SPI bus, one column at a time
* starting from the left-most column, each column sent from bottom to top. The bits and bytes
* in each column are scrambled however and need to be unscrambled before placing them into the
* framebuffer.
*/
#include <math.h>
#include "qemu-common.h"
#include "ui/console.h"
#include "ui/pixel_ops.h"
#include "hw/ssi.h"
#include "pebble_snowy_display.h"
#include "pebble_snowy_display_overlays.h"
//#define DEBUG_PEBBLE_SNOWY_DISPLAY
#ifdef DEBUG_PEBBLE_SNOWY_DISPLAY
// NOTE: The usleep() helps the MacOS stdout from freezing when we have a lot of print out
#define DPRINTF(fmt, ...) \
do { printf("PEBBLE_SNOWY_DISPLAY: " fmt , ## __VA_ARGS__); \
usleep(1000); \
} while (0)
#else
#define DPRINTF(fmt, ...)
#endif
// Colors
#define SNOWY_COLOR_BLACK 0
#define SNOWY_COLOR_WHITE 0xFC
#define SNOWY_COLOR_RED 0xC0
#define SNOWY_COLOR_GREEN 0x30
#define SNOWY_COLOR_BLUE 0x0C
/* Various states the Display can be in */
typedef enum {
PSDISPLAYSTATE_PROGRAMMING,
PSDISPLAYSTATE_ACCEPTING_CMD,
PSDISPLAYSTATE_ACCEPTING_PARAM,
PSDISPLAYSTATE_ACCEPTING_SCENE_BYTE,
PSDISPLAYSTATE_ACCEPTING_FRAME_DATA, // used in PSDISPLAY_CMD_SET_1 command set
} PSDisplayState;
#ifdef DEBUG_PEBBLE_SNOWY_DISPLAY
static const char *s_display_state[] = {
"PSDISPLAYSTATE_PROGRAMMING",
"PSDISPLAYSTATE_ACCEPTING_CMD",
"PSDISPLAYSTATE_ACCEPTING_PARAM",
"PSDISPLAYSTATE_ACCEPTING_SCENE_BYTE",
"PSDISPLAYSTATE_ACCEPTING_FRAME_DATA"
};
#endif
// Which command set the FPGA is implementing
typedef enum {
PSDISPLAY_CMD_SET_UNKNOWN,
PSDISPLAY_CMD_SET_0, // Boot ROM built on Dec 10, 2014
PSDISPLAY_CMD_SET_1, // FW ROM built on Jan 9, 2015
} PDisplayCmdSet;
/* Commands for PSDISPLAY_CMD_SET_0. We accept these while in the
* PSDISPLAYSTATE_ACCEPTING_CMD state. These are implemented in the first boot loader
* ROM built Dec 2014 */
typedef enum {
PSDISPLAYCMD0_NULL = 0,
PSDISPLAYCMD0_SET_PARAMETER = 1,
PSDISPLAYCMD0_DISPLAY_OFF = 2,
PSDISPLAYCMD0_DISPLAY_ON = 3,
PSDISPLAYCMD0_DRAW_SCENE = 4
} PDisplayCmd0;
/* Commands for PSDISPLAY_CMD_SET_1. We accept these while in the
* PSDISPLAYSTATE_ACCEPTING_CMD state. These are implemented in the firmware
* buit Jan 2015 */
typedef enum {
PSDISPLAYCMD1_FRAME_BEGIN = 5,
} PDisplayCmd1;
// Scene numbers put into cmd_parameter and used by the PSDISPLAYCMD0_DRAW_SCENE command.
typedef enum {
PSDISPLAYSCENE_BLACK = 0,
PSDISPLAYSCENE_SPLASH = 1, // splash screen
PSDISPLAYSCENE_UPDATE = 2, // firmware update
PSDISPLAYSCENE_ERROR = 3 // display error code
} PDisplayScene;
typedef struct {
SSISlave ssidev;
// -----------------------------------------------------------------
// Properties
union {
void *vdone_output;
qemu_irq done_output;
};
// This output line gets asserted (low) when we are done processing a drawing command.
// It is generally to an IRQ
union {
void *vintn_output;
qemu_irq intn_output;
};
uint32_t num_rows;
uint32_t num_cols;
int32_t num_border_rows;
int32_t num_border_cols;
uint8_t row_major;
uint8_t row_inverted;
uint8_t col_inverted;
uint8_t round_mask;
// -------------------------------------------------------------------
// Other state variables
QemuConsole *con;
bool redraw;
uint32_t bytes_per_row;
uint32_t bytes_per_frame;
uint8_t *framebuffer;
uint8_t *framebuffer_copy;
int col_index;
int row_index;
bool backlight_enabled;
float brightness;
bool power_on;
/* State variables */
PSDisplayState state;
uint8_t cmd;
uint32_t parameter;
uint32_t parameter_byte_offset;
PDisplayScene scene;
bool sclk_value;
bool cs_value; // low means asserted
bool vibrate_on;
int vibrate_offset;
/* We capture the first 256 bytes of the programming and inspect it to try and figure
* out which command set to expect */
uint8_t prog_header[256];
uint32_t prog_byte_offset;
// Which command set we are emulating
PDisplayCmdSet cmd_set;
} PSDisplayGlobals;
static uint8_t *get_pebble_logo_4colors_image(int *width, int *height);
static uint8_t *get_dead_face_image(int *width, int *height);
static uint8_t *get_small_pebble_logo_image(int *width, int *height);
static uint8_t *get_url_image(int *width, int *height);
static uint8_t *get_pixel_mask(void);
static uint32_t display_bytes = 0;
static uint32_t frameno = 0;
static void ps_set_state(PSDisplayGlobals *s, PSDisplayState new_state) {
DPRINTF("state change from %u (%s) to %u (%s) frame: %u - bytes: %u\n",
s->state, s_display_state[s->state], new_state, s_display_state[new_state], frameno, display_bytes);
s->state = new_state;
}
// -----------------------------------------------------------------------------
static void ps_set_redraw(PSDisplayGlobals *s) {
s->redraw = true;
memmove(s->framebuffer_copy, s->framebuffer, s->bytes_per_frame);
}
// -----------------------------------------------------------------------------
static void ps_display_set_pixel(PSDisplayGlobals *s, uint32_t x, uint32_t y,
uint8_t pixel_byte) {
s->framebuffer[y * s->bytes_per_row + x] = pixel_byte;
}
// -----------------------------------------------------------------------------
// Draw an 8-bits per pixel bitmap to the framebuffer
static void ps_display_draw_8bpp_bitmap(PSDisplayGlobals *s, uint8_t *bytes,
uint32_t x_offset, uint32_t y_offset, uint32_t width, uint32_t height)
{
uint32_t pixels = width * height;
int i;
for (i = 0; i < pixels; ++i) {
uint8_t value = bytes[i];
uint8_t x = x_offset + (i % width);
uint8_t y = y_offset + (i / width);
ps_display_set_pixel(s, x, y, value);
}
}
/*
// -----------------------------------------------------------------------------
// Draw a 1-bit per pixel bitmap to the framebuffer
static void ps_display_draw_1bpp_bitmap(PSDisplayGlobals *s, uint8_t *bits,
uint32_t x_offset, uint32_t y_offset, uint32_t width, uint32_t height) {
uint32_t pixels = width * height;
for (int i = 0; i < pixels; ++i) {
bool value = bits[i / 8] & (1 << (i % 8));
uint8_t x = x_offset + (i % width);
uint8_t y = y_offset + (i / width);
if (value) {
ps_display_set_pixel(s, x, y, 0xFC);
} else {
ps_display_set_pixel(s, x, y, 0x00);
}
}
}
*/
/* -----------------------------------------------------------------------------
Scan through the first part of the programming data and try and determine which
command set the FPGA is implementing. Here is an example of the data comprising
the programming for PSDISPLAY_CMD_SET_BOOT_0:
39F0: FF 00 4C 61 74 74 69 63 65 00 69 43 45 63 pG..Lattice.iCEc
3A00: 75 62 65 32 20 32 30 31 34 2E 30 38 2E 32 36 37 ube2 2014.08.267
3A10: 32 33 00 50 61 72 74 3A 20 69 43 45 34 30 4C 50 23.Part: iCE40LP
3A20: 31 4B 2D 43 4D 33 36 00 44 61 74 65 3A 20 44 65 1K-CM36.Date: De
3A30: 63 20 31 30 20 32 30 31 34 20 30 38 3A 33 30 3A c 10 2014 08:30:
3A40: 00 FF 31 38 00 7E AA 99 7E 51 00 01 05 92 00 20 ..18.~..~Q.....
3A50: 62 01 4B 72 00 90 82 00 00 11 00 01 01 00 00 00 b.Kr........... */
static void ps_display_determine_command_set(PSDisplayGlobals *s)
{
// Table of programming header dates and command sets
typedef struct {
const char *date_str;
PDisplayCmdSet cmd_set;
} CommandSetInfo;
static const CommandSetInfo cmd_sets[] = {
{"Date: Dec 10 2014 08:30", PSDISPLAY_CMD_SET_0},
{"Date: Jan 9 2015 10:49:47", PSDISPLAY_CMD_SET_1},
{"Date: Jan 30 2015 15:11:", PSDISPLAY_CMD_SET_1},
{"Date: Jan 30 2015 15:10:", PSDISPLAY_CMD_SET_1},
};
// Make sure prog_header is null terminated
if (s->prog_byte_offset >= sizeof(s->prog_header)) {
s->prog_byte_offset = sizeof(s->prog_header) - 1;
}
s->prog_header[s->prog_byte_offset] = 0;
// Default one to use
s->cmd_set = PSDISPLAY_CMD_SET_1;
// Skip first two bytes which contain 0xFF 00
int i;
for (i=2; i<s->prog_byte_offset; i++) {
const char *str_p = (const char *)s->prog_header + i;
DPRINTF("%s: found '%s' string in programming header\n", __func__, str_p);
// Look for a string that starts with "Date:"
if (!strncmp(str_p, "Date:", 5)) {
int n_cmd_sets = sizeof(cmd_sets) / sizeof(cmd_sets[0]);
int n;
for (n=0; n<n_cmd_sets; n++) {
if (!strncmp(str_p, cmd_sets[n].date_str, strlen(cmd_sets[n].date_str))) {
s->cmd_set = cmd_sets[n].cmd_set;
DPRINTF("%s: determined command set as %d\n", __func__, s->cmd_set);
return;
}
}
// We didn't find the command set, bail
DPRINTF("PEBBLE_SNOWY_DISPLAY: Unknown FPGA programming with a date"
" stamp of '%s'. Defaulting to command set %d\n", str_p, s->cmd_set);
return;
} else {
// Skip this string
i += strlen(str_p);
}
}
// Couldn't find the "Date:" string
DPRINTF("PEBBLE_SNOWY_DISPLAY: Error parsing FPGA programming data to"
" determine command set. Defaulting to command set %d\n", s->cmd_set);
return;
}
// -----------------------------------------------------------------------------
static void ps_display_reset_state(PSDisplayGlobals *s, bool assert_done)
{
// If we are resetting because we done with the previous command, assert done
if (assert_done) {
DPRINTF("Asserting done interrupt\n");
qemu_set_irq(s->intn_output, false);
}
DPRINTF("Resetting state to accept command\n");
ps_set_state(s, PSDISPLAYSTATE_ACCEPTING_CMD);
s->parameter_byte_offset = 0;
}
// -----------------------------------------------------------------------------
// Implements command PSDISPLAY_CMD_SET_0, used in the first boot ROM, built Dec 2014
static void ps_display_execute_current_cmd_set0(PSDisplayGlobals *s)
{
int width, height, x_offset, y_offset;
uint8_t *pixels;
DPRINTF("ps_display_execute_current_cmd_set0: cmd: %u -- cs: %s\n", s->cmd, s->cs_value ? "Not CS" : "CS");
switch (s->cmd) {
case PSDISPLAYCMD0_NULL:
DPRINTF("Executing command: NULL\n");
ps_display_reset_state(s, true /*assert_done*/);
break;
case PSDISPLAYCMD0_SET_PARAMETER:
DPRINTF("Executing command: SET_PARAMETER\n");
ps_set_state(s, PSDISPLAYSTATE_ACCEPTING_PARAM);
s->parameter_byte_offset = 0;
break;
case PSDISPLAYCMD0_DISPLAY_OFF:
DPRINTF("Executing command: DISPLAY_OFF\n");
ps_display_reset_state(s, true /*assert_done*/);
break;
case PSDISPLAYCMD0_DISPLAY_ON:
DPRINTF("Executing command: DISPLAY_ON\n");
ps_display_reset_state(s, true /*assert_done*/);
break;
case PSDISPLAYCMD0_DRAW_SCENE:
if (s->state == PSDISPLAYSTATE_ACCEPTING_CMD) {
ps_set_state(s, PSDISPLAYSTATE_ACCEPTING_SCENE_BYTE);
} else if (s->state == PSDISPLAYSTATE_ACCEPTING_SCENE_BYTE) {
DPRINTF("Executing command: DRAW_SCENE: %d\n", s->scene);
switch (s->scene) {
case PSDISPLAYSCENE_BLACK:
memset(s->framebuffer, SNOWY_COLOR_BLACK, s->bytes_per_frame);
break;
case PSDISPLAYSCENE_SPLASH:
pixels = get_pebble_logo_4colors_image(&width, &height);
x_offset = (s->num_cols - width)/2;
y_offset = (s->num_rows - height)/2;
ps_display_draw_8bpp_bitmap(s, pixels, x_offset, y_offset, width, height);
break;
case PSDISPLAYSCENE_UPDATE:
// NOTE: We do not yet support showing the progress bar, which should show
// the progress percent as indicated by s->parameter
pixels = get_small_pebble_logo_image(&width, &height);
x_offset = (s->num_cols - width)/2;
y_offset = s->num_rows - height - 88;
ps_display_draw_8bpp_bitmap(s, pixels, x_offset, y_offset, width, height);
break;
case PSDISPLAYSCENE_ERROR:
// NOTE: We do not yet support showing the error code, which should show
// the error in hex as indicated by s->parameter
pixels = get_dead_face_image(&width, &height);
x_offset = (s->num_cols - width)/2;
y_offset = s->num_rows - height - 92;
ps_display_draw_8bpp_bitmap(s, pixels, x_offset, y_offset, width, height);
pixels = get_url_image(&width, &height);
x_offset = (s->num_cols - width)/2;
y_offset = (s->num_rows - height - 10);
ps_display_draw_8bpp_bitmap(s, pixels, x_offset, y_offset, width, height);
break;
default:
DPRINTF("PEBBLE_SNOWY_DISPLAY: Unsupported scene: %d\n", s->scene);
break;
}
ps_display_reset_state(s, true /*assert_done*/);
ps_set_redraw(s);
} else {
DPRINTF("PEBBLE_SNOWY_DISPLAY: Tried to execute draw scene in "
"wrong state: %d\n", s->state);
ps_display_reset_state(s, true /*assert_done*/);
}
break;
default:
DPRINTF("PEBBLE_SNOWY_DISPLAY: Unsupported cmd: %d\n", s->cmd);
ps_display_reset_state(s, true /*assert_done*/);
break;
}
}
// -----------------------------------------------------------------------------
// Implements command set PSDISPLAY_CMD_SET_1, used in the firmware, built Jan 2015
static void ps_display_execute_current_cmd_set1(PSDisplayGlobals *s)
{
DPRINTF("ps_display_execute_current_cmd_set1: cmd: %u -- cs: %s\n", s->cmd, s->cs_value ? "Not CS" : "CS");
switch (s->cmd) {
case PSDISPLAYCMD1_FRAME_BEGIN:
DPRINTF("Executing command: FRAME_BEGIN\n");
if (s->row_major) {
if (s->row_inverted) {
// Expect to be sent rows, bottom to top
s->row_index = s->num_rows - s->num_border_rows - 1;
s->col_index = s->num_border_cols;
} else {
// Expect to be sent rows, top to bottom
s->row_index = s->num_border_rows;
s->col_index = s->num_border_cols;
}
} else {
// Expect to be sent columns, left to right
s->row_index = s->num_rows - s->num_border_rows - 1;
s->col_index = s->num_border_cols;
}
ps_set_state(s, PSDISPLAYSTATE_ACCEPTING_FRAME_DATA);
// Just say we are done immediately. We can accept more data right away.
qemu_set_irq(s->intn_output, false);
break;
default:
DPRINTF("PEBBLE_SNOWY_DISPLAY: Unsupported cmd: %d\n", s->cmd);
ps_display_reset_state(s, true /*assert_done*/);
break;
}
}
// -----------------------------------------------------------------------------
static void ps_display_cmd_set_2_unscramble_column(PSDisplayGlobals *s, uint32_t col_index)
{
int row_idx;
const int line_bytes = s->num_rows - 2 * s->num_border_rows;
uint8_t col_buffer[line_bytes];
// Copy the column into temp buffer first, without border pixels
for (row_idx = 0; row_idx < line_bytes; row_idx++) {
col_buffer[row_idx] = s->framebuffer[(row_idx + s->num_border_rows) * s->bytes_per_row
+ col_index];
}
// Unscramble the bytes in the scanline.
//
// In the desription below, the bits in the first pixel in a column are
// identified as follows:
// r0_msb r0_lsb g0_msb g0_lsb b0_msb b0_lsb 0 0
// The second pixel:
// r1_msb r1_lsb g1_msb g1_lsb b1_msb b1_lsb 0 0
//
// Each scan line contains N bytes of data for the N pixels.
// [LSB0 LSB2 .................LSBN MSB0 MSB2 ....................MSBN]
//
// LSB0 contains the following bits:
// 0 0 r1_lsb r0_lsb g1_lsb g0_lsb b1_lsb b0_lsb
// MSB0 contains the following bits:
// 0 0 r1_msb r0_msb g1_msb g0_msb b1_msb b0_msb
// LSB2 contains the following bits:
// 0 0 r3_lsb r2_lsb g3_lsb g2_lsb b3_lsb b2_lsb
// MSB2 contains the following bits:
// 0 0 r3_msb r2_msb g3_msb g2_msb b3_msb b2_msb
//
uint8_t ms_bits, ls_bits;
for (row_idx = 0; row_idx < line_bytes; row_idx += 2) {
ls_bits = col_buffer[row_idx/2];
ms_bits = col_buffer[row_idx/2 + line_bytes/2];
// Form the 2 pixels whose data is found in ls_bits and ms_bits
uint8_t pixel_0, pixel_1;
pixel_0 = ((ms_bits & 0b00010101) << 1) | (ls_bits & 0b00010101);
pixel_0 <<= 2;
pixel_1 = (ms_bits & 0b00101010) | ((ls_bits & 0b00101010) >> 1);
pixel_1 <<= 2;
// Write them to the frame buffer
s->framebuffer[(row_idx + s->num_border_rows) * s->bytes_per_row
+ col_index] = pixel_0;
s->framebuffer[(row_idx + s->num_border_rows + 1) * s->bytes_per_row
+ col_index] = pixel_1;
}
}
// -----------------------------------------------------------------------------
static void ps_display_cmd_set_2_unscramble_row(PSDisplayGlobals *s, uint32_t row_index)
{
const int line_bytes = s->num_cols - 2 * s->num_border_cols;
uint8_t row_buffer[line_bytes];
uint8_t *fb = &s->framebuffer[row_index * s->bytes_per_row + s->num_border_cols];
// Copy the row into temp buffer first, without border pixels
memcpy(row_buffer, fb, line_bytes);
// Unscramble the bytes in the scanline.
//
// In the desription below, the bits in the first pixel in a column are
// identified as follows:
// r0_msb r0_lsb g0_msb g0_lsb b0_msb b0_lsb 0 0
// The second pixel:
// r1_msb r1_lsb g1_msb g1_lsb b1_msb b1_lsb 0 0
//
// Each scan line contains N bytes of data for the N pixels.
// [LSB0 LSB2 .................LSBN MSB0 MSB2 ....................MSBN]
//
// LSB0 contains the following bits:
// 0 0 r1_lsb r0_lsb g1_lsb g0_lsb b1_lsb b0_lsb
// MSB0 contains the following bits:
// 0 0 r1_msb r0_msb g1_msb g0_msb b1_msb b0_msb
// LSB2 contains the following bits:
// 0 0 r3_lsb r2_lsb g3_lsb g2_lsb b3_lsb b2_lsb
// MSB2 contains the following bits:
// 0 0 r3_msb r2_msb g3_msb g2_msb b3_msb b2_msb
//
for (int col_idx = 0; col_idx < line_bytes; col_idx += 2) {
const uint8_t ms_bits = row_buffer[col_idx / 2];
const uint8_t ls_bits = row_buffer[col_idx / 2 + line_bytes / 2];
// Form the 2 pixels whose data is found in ls_bits and ms_bits
uint8_t pixel_0, pixel_1;
pixel_0 = ((ms_bits & 0b00010101) << 1) | (ls_bits & 0b00010101);
pixel_0 <<= 2;
pixel_1 = (ms_bits & 0b00101010) | ((ls_bits & 0b00101010) >> 1);
pixel_1 <<= 2;
// Write them to the frame buffer
if (s->col_inverted) {
fb[line_bytes - col_idx - 2] = pixel_0;
fb[line_bytes - col_idx - 1] = pixel_1;
} else {
fb[col_idx] = pixel_1;
fb[col_idx + 1] = pixel_0;
}
}
}
static bool newdisp = true;
// -----------------------------------------------------------------------------
static uint32_t ps_display_transfer(SSISlave *dev, uint32_t data)
{
++display_bytes;
PSDisplayGlobals *s = FROM_SSI_SLAVE(PSDisplayGlobals, dev);
uint32_t data_byte = data & 0x00FF;
//DPRINTF("rcv byte: 0x%02x\n", data_byte);
/* Ignore incoming data if our chip select is not asserted */
if (s->cs_value) {
if (s->state != PSDISPLAYSTATE_PROGRAMMING) {
DPRINTF("PEBBLE_SNOWY_DISPLAY: received data without CS asserted");
}
return 0;
}
switch(s->state) {
case PSDISPLAYSTATE_PROGRAMMING:
// Capture the start of the programming data
if (s->prog_byte_offset < sizeof(s->prog_header)) {
s->prog_header[s->prog_byte_offset++] = data_byte;
}
break;
case PSDISPLAYSTATE_ACCEPTING_CMD:
s->cmd = data_byte;
DPRINTF("received command %d, deasserting done interrupt\n", s->cmd);
// Start of a command. Deassert done interrupt, it will get asserted again when
// ps_display_reset_state() is called at the end of the command
qemu_set_irq(s->intn_output, true);
if (s->cmd_set == PSDISPLAY_CMD_SET_0) {
ps_display_execute_current_cmd_set0(s);
} else if (s->cmd_set == PSDISPLAY_CMD_SET_1) {
ps_display_execute_current_cmd_set1(s);
} else {
DPRINTF("Unimplemeneted command set\n");
abort();
}
break;
case PSDISPLAYSTATE_ACCEPTING_PARAM:
DPRINTF("received param byte %d\n", data_byte);
/* Params are sent low byte first */
if (s->parameter_byte_offset == 0) {
s->parameter = (s->parameter & 0xFFFFFF00) | data_byte;
} else if (s->parameter_byte_offset == 1) {
s->parameter = (s->parameter & 0xFFFF00FF) | (data_byte << 8);
} else if (s->parameter_byte_offset == 2) {
s->parameter = (s->parameter & 0xFF00FFFF) | (data_byte << 16);
} else if (s->parameter_byte_offset == 3) {
s->parameter = (s->parameter & 0x00FFFFFF) | (data_byte << 24);
} else {
DPRINTF("PEBBLE_SNOWY_DISPLAY: received more than 4 bytes of parameter");
}
s->parameter_byte_offset++;
if (s->parameter_byte_offset >= 4) {
DPRINTF("assembled complete param value of %d\n", s->parameter);
ps_display_reset_state(s, true /*assert_done*/);
}
break;
case PSDISPLAYSTATE_ACCEPTING_SCENE_BYTE:
s->scene = data_byte;
DPRINTF("received scene ID: %d\n", s->scene);
ps_display_execute_current_cmd_set0(s);
break;
case PSDISPLAYSTATE_ACCEPTING_FRAME_DATA:
if (newdisp) {
DPRINTF("New frame? -- 0x%02X, row %d, col %d -- bytes: %u -- cs: %s\n", data, s->row_index, s->col_index, display_bytes, s->cs_value ? "Not CS" : "CS");
newdisp = false;
}
s->framebuffer[s->row_index * s->bytes_per_row + s->col_index] = data;
if (s->row_major) {
// We get sent one row at a time
s->col_index++;
if (s->col_index >= s->num_cols - s->num_border_cols) {
// Reached end of row, unscramble the one we just received and go onto the
// next row
ps_display_cmd_set_2_unscramble_row(s, s->row_index);
s->col_index = s->num_border_cols;
bool got_last_byte;
if (s->row_inverted) {
s->row_index--;
got_last_byte = (s->row_index < s->num_border_rows);
} else {
s->row_index++;
got_last_byte = (s->row_index >= s->num_rows - s->num_border_rows);
}
if (got_last_byte) {
DPRINTF("Got last byte in frame (row) row %d, col %d -- bytes: %u\n",
s->row_index, s->col_index, display_bytes);
ps_set_state(s, PSDISPLAYSTATE_ACCEPTING_CMD);
ps_set_redraw(s);
newdisp = true;
++frameno;
}
}
} else {
// We get sent one column at a time
s->row_index--;
if (s->row_index < s->num_border_rows) {
// Reached top of column, unscramble the one we just received and go onto the
// next column
ps_display_cmd_set_2_unscramble_column(s, s->col_index);
s->row_index = s->num_rows - s->num_border_rows - 1;
s->col_index += 1;
if (s->col_index >= s->num_cols - s->num_border_cols) {
DPRINTF("Got last byte in frame (col) row %d, col %d\n", s->row_index, s->col_index);
ps_set_state(s, PSDISPLAYSTATE_ACCEPTING_CMD);
ps_set_redraw(s);
newdisp = true;
++frameno;
}
}
}
break;
}
return 0;
}
// -----------------------------------------------------------------------------
// This function maps an 8 bit value from the frame buffer into red, green, and blue
// components
static PSDisplayPixelColor ps_display_get_rgb(PSDisplayGlobals *s, uint8_t pixel_value) {
PSDisplayPixelColor c;
c.red = ((pixel_value & 0xC0) >> 6) * 255 / 3;
c.green = ((pixel_value & 0x30) >> 4) * 255 / 3;
c.blue = ((pixel_value & 0x0C) >> 2) * 255 / 3;
// Adjust the pixel RGB to compensate for the set brightness.
// brightness = 0: 255 in maps to 170 out
// brightness = 1.0: 255 in maps to 255 out
float brightness = s->backlight_enabled ? s->brightness : 0.0;
int max_val = 170 + (255 - 170) * brightness;
c.red = (int)c.red * max_val/255;
c.green = (int)c.green * max_val/255;
c.blue = (int)c.blue * max_val/255;
return c;
/*
static PSDisplayPixelColors[256] = {
{0, 0, 0 },
};
return PSDisplayPixelColors[pixel_value];
*/
}
// -----------------------------------------------------------------------------
static void ps_display_update_display(void *arg)
{
PSDisplayGlobals *s = arg;
uint8_t *d;
int x, y, bpp, rgb_value;
DisplaySurface *surface = qemu_console_surface(s->con);
bpp = surface_bits_per_pixel(surface);
d = surface_data(surface);
// If vibrate is on, simply jiggle the display
if (s->vibrate_on) {
if (s->vibrate_offset == 0) {
s->vibrate_offset = 2;
}
int bytes_per_pixel;
switch (bpp) {
case 8:
bytes_per_pixel = 1;
break;
case 15:
case 16:
bytes_per_pixel = 2;
break;
case 32:
bytes_per_pixel = 4;
break;
default:
abort();
}
int total_bytes = s->num_rows * s->num_cols * bytes_per_pixel
- abs(s->vibrate_offset) * bytes_per_pixel;
if (s->vibrate_offset > 0) {
memmove(d, d + s->vibrate_offset * bytes_per_pixel, total_bytes);
} else {
memmove(d - s->vibrate_offset * bytes_per_pixel, d, total_bytes);
}
s->vibrate_offset *= -1;
dpy_gfx_update(s->con, 0, 0, s->num_cols, s->num_rows);
return;
}
if (!s->redraw) {
return;
}
const bool is_spalding = s->round_mask;
const PSDisplayPixelColorWithAlpha *overlay = is_spalding ? g_spalding_overlay : NULL;
uint8_t *pixel_mask = get_pixel_mask();
for (y = 0; y < s->num_rows; y++) {
for (x = 0; x < s->num_cols; x++) {
uint32_t offset = y * s->bytes_per_row + x;
uint8_t pixel = s->framebuffer_copy[offset];
if (s->round_mask) {
// Compute the vertical distance from top or bottom edge, whichever is closest
int vert_distance = y;
if (vert_distance >= s->num_rows/2) {
vert_distance = s->num_rows - 1 - y ;
}
int mask_width = pixel_mask[vert_distance];
if (x < mask_width || x >= s->num_cols - mask_width) {
pixel = 0;
}
}
PSDisplayPixelColor color = ps_display_get_rgb(s, pixel);
if (overlay) {
const PSDisplayPixelColorWithAlpha blend_color = overlay[offset];
const int32_t factor_over = blend_color.alpha;
const int32_t factor_dest = 255 - blend_color.alpha;
color.red = MIN(255, (factor_over * blend_color.color.red + factor_dest * color.red) / 255);
color.green = MIN(255,(factor_over * blend_color.color.green + factor_dest * color.green) / 255);
color.blue = MIN(255, (factor_over * blend_color.color.blue + factor_dest * color.blue) / 255);
}
switch(bpp) {
case 8:
*((uint8_t *)d) = rgb_to_pixel8(color.red, color.green, color.blue);
d++;
break;
case 15:
*((uint16_t *)d) = rgb_to_pixel15(color.red, color.green, color.blue);;
d += 2;
break;
case 16:
*((uint16_t *)d) = rgb_to_pixel16(color.red, color.green, color.blue);;
d += 2;
break;
case 24:
rgb_value = rgb_to_pixel24(color.red, color.green, color.blue);
*d++ = (rgb_value & 0x00FF0000) >> 16;
*d++ = (rgb_value & 0x0000FF00) >> 8;
*d++ = (rgb_value & 0x000000FF);
break;
case 32:
*((uint32_t *)d) = rgb_to_pixel32(color.red, color.green, color.blue);;
d += 4;
break;
}
}
}
dpy_gfx_update(s->con, 0, 0, s->num_cols, s->num_rows);
s->redraw = false;
}
// -----------------------------------------------------------------------------
static int ps_display_set_cs(SSISlave *dev, bool value)
{
PSDisplayGlobals *s = FROM_SSI_SLAVE(PSDisplayGlobals, dev);
DPRINTF("CS changed to %d\n", value);
s->cs_value = value;
// When CS goes up (unasserted), we are done programming
if (value && s->state == PSDISPLAYSTATE_PROGRAMMING) {
DPRINTF("Exiting programming mode\n");
ps_display_reset_state(s, true /*assert_done*/);
// Try and figure out which command set the FPGA expects by parsing the
// programming data
ps_display_determine_command_set(s);
}
return 0;
}
// -----------------------------------------------------------------------------
static void ps_display_set_reset_pin_cb(void *opaque, int n, int level)
{
PSDisplayGlobals *s = FROM_SSI_SLAVE(PSDisplayGlobals, opaque);
bool value = !!level;
assert(n == 0);
DPRINTF("RESET changed to %d\n", value);
qemu_set_irq(s->done_output, false);
// When reset goes high, sample the CS input to see what state we should be in
// if CS is low, expect new FPGA programming to arrive
// if CS is high, assume we will be using the bootloader configuration
if (value) {
// After a reset, we are not done. De-assert our interrupt (asserted low).
qemu_set_irq(s->intn_output, true);
if (s->cs_value) {
// Assume we are using the bootloader configuration
s->cmd_set = PSDISPLAY_CMD_SET_0;
ps_display_reset_state(s, true);
} else {
ps_set_state(s, PSDISPLAYSTATE_PROGRAMMING);
s->prog_byte_offset = 0;
}
}
}
// -----------------------------------------------------------------------------
static void ps_display_set_sclk_pin_cb(void *opaque, int n, int level)
{
PSDisplayGlobals *s = FROM_SSI_SLAVE(PSDisplayGlobals, opaque);
assert(n == 0);
/* Save new value */
s->sclk_value = !!level;
}
// -----------------------------------------------------------------------------
static void ps_display_invalidate_display(void *arg)
{
PSDisplayGlobals *s = arg;
s->redraw = true;
}
// -----------------------------------------------------------------------------
static const GraphicHwOps ps_display_ops =
{
.gfx_update = ps_display_update_display,
.invalidate = ps_display_invalidate_display,
};
// -----------------------------------------------------------------------------
static void ps_display_backlight_enable_cb(void *opaque, int n, int level)
{
PSDisplayGlobals *s = (PSDisplayGlobals *)opaque;
assert(n == 0);
bool enable = (level != 0);
if (s->backlight_enabled != enable) {
s->backlight_enabled = enable;
s->redraw = true;
}
}
// -----------------------------------------------------------------------------
// Set brightness, from 0 to 255
static void ps_display_set_backlight_level_cb(void *opaque, int n, int level)
{
PSDisplayGlobals *s = (PSDisplayGlobals *)opaque;
assert(n == 0);
float bright_f = (float)level / 255;
// Temp hack - the Pebble sets the PWM to 25% for max brightness
float new_setting = MIN(1.0, bright_f * 4);
if (new_setting != s->brightness) {
s->brightness = MIN(1.0, bright_f * 4);
if (s->backlight_enabled) {
s->redraw = true;
}
}
}
// -----------------------------------------------------------------------------
static void ps_display_vibe_ctl(void *opaque, int n, int level)
{
PSDisplayGlobals *s = (PSDisplayGlobals *)opaque;
assert(n == 0);
s->vibrate_on = (level != 0);
s->redraw = true;
}
// -----------------------------------------------------------------------------
static void ps_display_power_ctl(void *opaque, int n, int level)
{
PSDisplayGlobals *s = (PSDisplayGlobals *)opaque;
assert(n == 0);
if (!level && s->power_on) {
memset(s->framebuffer, 0, s->bytes_per_frame);
ps_set_redraw(s);
s->power_on = false;
}
s->power_on = !!level;
}
// -----------------------------------------------------------------------------
static void ps_display_reset(DeviceState *dev)
{
PSDisplayGlobals *s = (PSDisplayGlobals *)dev;
memset(s->framebuffer, 0, s->bytes_per_frame);
ps_set_redraw(s);
}
// -----------------------------------------------------------------------------
static int ps_display_init(SSISlave *dev)
{
PSDisplayGlobals *s = FROM_SSI_SLAVE(PSDisplayGlobals, dev);