forked from WartyMN/F256-FileManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtext.c
1530 lines (1223 loc) · 55.1 KB
/
text.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
/*
* text.c
*
* Created on: Feb 19, 2022
* Author: micahbly
*/
// This is a cut-down, semi-API-compatible version of the OS/f text.c file from Lich King (Foenix)
// adapted for Foenix F256 Jr starting November 29, 2022
/*****************************************************************************/
/* Includes */
/*****************************************************************************/
// project includes
#include "app.h"
#include "text.h"
#include "general.h"
#include "keyboard.h"
#include "sys.h"
// C includes
#include <stdbool.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// F256 includes
#include "f256.h"
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
/*****************************************************************************/
/* Enumerations */
/*****************************************************************************/
/*****************************************************************************/
/* File-scoped Variables */
/*****************************************************************************/
uint8_t last_x;
uint8_t last_y;
/*****************************************************************************/
/* Global Variables */
/*****************************************************************************/
extern System* global_system;
/*****************************************************************************/
/* Private Function Prototypes */
/*****************************************************************************/
//! Validate the coordinates are within the bounds of the specified screen.
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the horizontal position to validate. Must be between 0 and the screen's text_cols_vis_ - 1
//! @param y: the vertical position to validate. Must be between 0 and the screen's text_rows_vis_ - 1
bool Text_ValidateXY(int8_t x, int8_t y);
// Fill attribute or text char memory. Writes to char memory if for_attr is false.
// calling function must validate the screen ID before passing!
//! @return Returns false on any error/invalid input.
bool Text_FillMemory(bool for_attr, uint8_t the_fill);
//! Fill character and attribute memory for a specific box area
//! calling function must validate screen id, coords, attribute value before passing!
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param width: width, in character cells, of the rectangle to be filled
//! @param height: height, in character cells, of the rectangle to be filled
//! @param the_attribute_value: a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return Returns false on any error/invalid input.
bool Text_FillMemoryBoxBoth(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t the_char, uint8_t the_attribute_value);
//! Fill character OR attribute memory for a specific box area
//! calling function must validate screen id, coords, attribute value before passing!
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param width: width, in character cells, of the rectangle to be filled
//! @param height: height, in character cells, of the rectangle to be filled
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return Returns false on any error/invalid input.
bool Text_FillMemoryBox(uint8_t x, uint8_t y, uint8_t width, uint8_t height, bool for_attr, uint8_t the_fill);
/*****************************************************************************/
/* Private Function Definitions */
/*****************************************************************************/
// **** NOTE: all functions in private section REQUIRE pre-validated parameters.
// **** NEVER call these from your own functions. Always use the public interface. You have been warned!
//! Fill attribute or text char memory.
//! calling function must validate the screen ID before passing!
//! @param the_screen: valid pointer to the target screen to operate on
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return Returns false on any error/invalid input.
bool Text_FillMemory(bool for_attr, uint8_t the_fill)
{
uint16_t the_write_len;
uint8_t* the_write_loc;
// LOGIC:
// On F256jr, the write len and write locs are same for char and attr memory, difference is IO page 2 or 3
if (for_attr)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
}
else
{
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
}
the_write_len = SCREEN_TOTAL_BYTES;
the_write_loc = (uint8_t*)SCREEN_TEXT_MEMORY_LOC;
memset(the_write_loc, the_fill, the_write_len);
Sys_RestoreIOPage();
//printf("Text_FillMemory: done \n");
//DEBUG_OUT(("%s %d: done (for_attr=%u, the_fill=%u)", __func__, __LINE__, for_attr, the_fill));
return true;
}
//! Fill character and attribute memory for a specific box area
//! calling function must validate screen id, coords, attribute value before passing!
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param width: width, in character cells, of the rectangle to be filled
//! @param height: height, in character cells, of the rectangle to be filled
//! @param the_attribute_value: a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return Returns false on any error/invalid input.
bool Text_FillMemoryBoxBoth(uint8_t x, uint8_t y, uint8_t width, uint8_t height, uint8_t the_char, uint8_t the_attribute_value)
{
uint8_t* the_write_loc;
uint8_t max_row;
// LOGIC:
// On F256jr, the write len and write locs are same for char and attr memory, difference is IO page 2 or 3
// set up initial loc
the_write_loc = Text_GetMemLocForXY(x, y);
max_row = y + height;
for (; y <= max_row; y++)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
memset(the_write_loc, the_attribute_value, width);
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
memset(the_write_loc, the_char, width);
the_write_loc += SCREEN_NUM_COLS;
}
Sys_RestoreIOPage();
return true;
}
//! Fill character OR attribute memory for a specific box area
//! calling function must validate screen id, coords, attribute value before passing!
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param width: width, in character cells, of the rectangle to be filled
//! @param height: height, in character cells, of the rectangle to be filled
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @param the_fill: either a 1-byte character code, or a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return Returns false on any error/invalid input.
bool Text_FillMemoryBox(uint8_t x, uint8_t y, uint8_t width, uint8_t height, bool for_attr, uint8_t the_fill)
{
uint8_t* the_write_loc;
uint8_t max_row;
// LOGIC:
// On F256jr, the write len and write locs are same for char and attr memory, difference is IO page 2 or 3
if (for_attr)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
}
else
{
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
}
// set up initial loc
the_write_loc = Text_GetMemLocForXY(x, y);
max_row = y + height;
for (; y <= max_row; y++)
{
memset(the_write_loc, the_fill, width);
the_write_loc += SCREEN_NUM_COLS;
}
Sys_RestoreIOPage();
return true;
}
/*****************************************************************************/
/* Public Function Definitions */
/*****************************************************************************/
// ** NOTE: there is no destructor or constructor for this library, as it does not track any allocated memory. It works on the basis of a screen ID, which corresponds to the text memory for Vicky's Channel A and Channel B video memory.
// **** Block copy functions ****
//! Copy a rectangular area of text or attr to or from a linear memory buffer.
//! Use this if you do not have a full-sized (screen-size) off-screen buffer, but instead have a block perhaps just big enough to hold the rect.
//! @param the_screen: valid pointer to the target screen to operate on
//! @param the_buffer: valid pointer to a block of memory to hold (or alternatively act as the source of) the character or attribute data for the specified rectangle of screen memory. This will be read from first byte to last byte, without skipping. e.g., if you want to copy a 40x5 rectangle of text from the middle of the screen to this buffer, the buffer must be 40*5=200 bytes in length, and data will be written contiguously to it.
//! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param to_screen: true to copy to the screen from the buffer, false to copy from the screen to the buffer. Recommend using SCREEN_COPY_TO_SCREEN/SCREEN_COPY_FROM_SCREEN.
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
//! @return Returns false on any error/invalid input.
bool Text_CopyMemBoxLinearBuffer(uint8_t* the_buffer, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, bool to_screen, bool for_attr)
{
uint8_t* the_vram_loc;
uint8_t* the_buffer_loc;
uint8_t the_write_len;
int16_t initial_offset;
// LOGIC:
// On F256jr, the write len and write locs are same for char and attr memory, difference is IO page 2 or 3
// adjust the x, y, x2, y2, so that we are never trying to copy out of the physical screen box
if (x2 < x1)
{
x2 = x1;
}
if (y2 < y1)
{
y2 = y1;
}
if (x1 > SCREEN_LAST_COL)
{
return false;
}
if (x2 > SCREEN_LAST_COL)
{
x2 = SCREEN_LAST_COL;
}
if (y1 > SCREEN_LAST_ROW)
{
return false;
}
if (y2 > SCREEN_LAST_ROW)
{
y2 = SCREEN_LAST_ROW;
}
if (for_attr)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
}
else
{
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
}
// get initial read/write locs
initial_offset = (SCREEN_NUM_COLS * y1) + x1;
the_buffer_loc = the_buffer;
the_write_len = x2 - x1 + 1;
the_vram_loc = (uint8_t*)SCREEN_TEXT_MEMORY_LOC + initial_offset;
// do copy one line at a time
//DEBUG_OUT(("%s %d: vramloc=%p, buffer=%p, bufferloc=%p, to_screen=%i, the_write_len=%i", the_vram_loc, the_buffer, the_buffer_loc, to_screen, the_write_len));
for (; y1 <= y2; y1++)
{
if (to_screen)
{
memcpy(the_vram_loc, the_buffer_loc, the_write_len);
}
else
{
memcpy(the_buffer_loc, the_vram_loc, the_write_len);
}
the_buffer_loc += the_write_len;
the_vram_loc += SCREEN_NUM_COLS;
}
Sys_RestoreIOPage();
return true;
}
// //! Copy a rectangular area of text or attr to or from an off-screen buffer of the same size as the physical screen buffer
// //! @param the_screen: valid pointer to the target screen to operate on
// //! @param the_buffer: valid pointer to a block of memory to hold (or alternatively act as the source of) the character or attribute data for the specified rectangle of screen memory. This buffer must be the same size as the physical screen!
// //! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
// //! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
// //! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
// //! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
// //! @param to_screen: true to copy to the screen from the buffer, false to copy from the screen to the buffer. Recommend using SCREEN_COPY_TO_SCREEN/SCREEN_COPY_FROM_SCREEN.
// //! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
// //! @return Returns false on any error/invalid input.
// bool Text_CopyMemBox(uint8_t* the_buffer, uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, bool to_screen, bool for_attr)
// {
// uint8_t* the_vram_loc;
// uint8_t* the_buffer_loc;
// uint8_t the_write_len;
// int16_t initial_offset;
//
// // LOGIC:
// // On F256jr, the write len and write locs are same for char and attr memory, difference is IO page 2 or 3
//
// if (for_attr)
// {
// Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
// }
// else
// {
// Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
// }
//
// // get initial read/write locs
// initial_offset = (SCREEN_NUM_COLS * y1) + x1;
// the_buffer_loc = the_buffer + initial_offset;
// the_write_len = x2 - x1 + 1;
//
// the_vram_loc = (uint8_t*)SCREEN_TEXT_MEMORY_LOC + initial_offset;
//
// // do copy one line at a time
//
// //DEBUG_OUT(("%s %d: vramloc=%p, buffer=%p, bufferloc=%p, to_screen=%i, the_write_len=%i", the_vram_loc, the_buffer, the_buffer_loc, to_screen, the_write_len));
//
// for (; y1 <= y2; y1++)
// {
// if (to_screen)
// {
// memcpy(the_vram_loc, the_buffer_loc, the_write_len);
// }
// else
// {
// memcpy(the_buffer_loc, the_vram_loc, the_write_len);
// }
//
// the_buffer_loc += SCREEN_NUM_COLS;
// the_vram_loc += SCREEN_NUM_COLS;
// }
//
// Sys_RestoreIOPage();
//
// return true;
// }
// **** Block fill functions ****
//! Clear the text screen and reset foreground and background colors
void Text_ClearScreen(uint8_t fore_color, uint8_t back_color)
{
uint8_t the_attribute_value;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
//DEBUG_OUT(("%s %d: the_attribute_value=%u", __func__, __LINE__, the_attribute_value));
Text_FillMemory(SCREEN_FOR_TEXT_CHAR, ' ');
Text_FillMemory(SCREEN_FOR_TEXT_ATTR, the_attribute_value);
}
//! Fill character and attribute memory for a specific box area
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_char: the character to be used for the fill operation
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return Returns false on any error/invalid input.
bool Text_FillBox(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t the_char, uint8_t fore_color, uint8_t back_color)
{
uint8_t dy;
uint8_t dx;
uint8_t the_attribute_value;
// add 1 to H line len, because dx becomes width, and if width = 0, then memset gets 0, and nothing happens.
// dy can be 0 and you still get at least one row done.
dx = x2 - x1 + 1;
dy = y2 - y1 + 0;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
return Text_FillMemoryBoxBoth(x1, y1, dx, dy, the_char, the_attribute_value);
}
// //! Fill character memory for a specific box area
// //! @param the_screen: valid pointer to the target screen to operate on
// //! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
// //! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
// //! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
// //! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
// //! @param the_char: the character to be used for the fill operation
// //! @return Returns false on any error/invalid input.
// bool Text_FillBoxCharOnly(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t the_char)
// {
// uint8_t dy;
// uint8_t dx;
//
// if (x1 > x2 || y1 > y2)
// {
// LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
// return false;
// }
//
// // add 1 to H line len, because dx becomes width, and if width = 0, then memset gets 0, and nothing happens.
// // dy can be 0 and you still get at least one row done.
// dx = x2 - x1 + 1;
// dy = y2 - y1 + 0;
//
// return Text_FillMemoryBox(x1, y1, dx, dy, SCREEN_FOR_TEXT_CHAR, the_char);
// }
//! Fill attribute memory for a specific box area
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return Returns false on any error/invalid input.
bool Text_FillBoxAttrOnly(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t fore_color, uint8_t back_color)
{
uint8_t dy;
uint8_t dx;
uint8_t the_attribute_value;
if (x1 > x2 || y1 > y2)
{
LOG_ERR(("%s %d: illegal coordinates", __func__, __LINE__));
return false;
}
// add 1 to H line len, because dx becomes width, and if width = 0, then memset gets 0, and nothing happens.
// dy can be 0 and you still get at least one row done.
dx = x2 - x1 + 1;
dy = y2 - y1 + 0;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
return Text_FillMemoryBox(x1, y1, dx, dy, SCREEN_FOR_TEXT_ATTR, the_attribute_value);
}
//! Invert the colors of a rectangular block.
//! As this requires sampling each character cell, it is no faster (per cell) to do for entire screen as opposed to a subset box
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @return Returns false on any error/invalid input.
bool Text_InvertBox(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2)
{
uint8_t the_attribute_value;
uint8_t the_inversed_value;
uint8_t* the_write_loc;
uint8_t the_col;
uint8_t skip_len;
uint8_t back_nibble;
uint8_t fore_nibble;
// get initial read/write loc
the_write_loc = Text_GetMemLocForXY(x1, y1);
// amount of cells to skip past once we have written the specified line len
skip_len = SCREEN_NUM_COLS - (x2 - x1) - 1;
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
for (; y1 <= y2; y1++)
{
for (the_col = x1; the_col <= x2; the_col++)
{
the_attribute_value = (unsigned char)*the_write_loc;
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
back_nibble = ((the_attribute_value & 0xF0) >> 4);
fore_nibble = ((the_attribute_value & 0x0F) << 4);
the_inversed_value = (fore_nibble | back_nibble);
*the_write_loc++ = the_inversed_value;
}
the_write_loc += skip_len;
}
Sys_RestoreIOPage();
return true;
}
// **** Set char/attr functions *****
// NOTE: all functions from here lower that pass an x/y will update the last_x/last_y parameters.
//! Set a char at a specified x, y coord
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_char: the character to be used
//! @return Returns false on any error/invalid input.
bool Text_SetCharAtXY(uint8_t x, uint8_t y, uint8_t the_char)
{
uint8_t* the_write_loc;
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
the_write_loc = Text_GetMemLocForXY(x, y);
*the_write_loc = the_char;
Sys_RestoreIOPage();
last_x = x;
last_y = y;
return true;
}
//! Set the attribute value at a specified x, y coord
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_attribute_value: a 1-byte attribute code (foreground in high nibble, background in low nibble)
//! @return Returns false on any error/invalid input.
bool Text_SetAttrAtXY(uint8_t x, uint8_t y, uint8_t the_attribute_value)
{
uint8_t* the_write_loc;
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
the_write_loc = Text_GetMemLocForXY(x, y);
*the_write_loc = the_attribute_value;
Sys_RestoreIOPage();
last_x = x;
last_y = y;
return true;
}
//! Set the attribute value at a specified x, y coord based on the foreground and background colors passed
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return Returns false on any error/invalid input.
bool Text_SetColorAtXY(uint8_t x, uint8_t y, uint8_t fore_color, uint8_t back_color)
{
uint8_t the_attribute_value;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
last_x = x;
last_y = y;
return Text_SetAttrAtXY(x, y, the_attribute_value);
}
// //! Draw a char at a specified x, y coord, also setting the color attributes
// //! @param the_screen: valid pointer to the target screen to operate on
// //! @param x: the horizontal position, between 0 and the screen's text_cols_vis_ - 1
// //! @param y: the vertical position, between 0 and the screen's text_rows_vis_ - 1
// //! @param the_char: the character to be used
// //! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
// //! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
// //! @return Returns false on any error/invalid input.
// bool Text_SetCharAndAttrAtXY(uint8_t x, uint8_t y, uint8_t the_char, uint8_t the_attribute_value)
// {
// uint8_t* the_write_loc;
//
// the_write_loc = Text_GetMemLocForXY(x, y);
//
// Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
// *the_write_loc = the_attribute_value;
// Sys_RestoreIOPage();
//
// Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
// *the_write_loc = the_char;
// Sys_RestoreIOPage();
//
// last_x = x;
// last_y = y;
//
// return true;
// }
//! Draw a char at a specified x, y coord, also setting the color attributes
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_char: the character to be used
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return Returns false on any error/invalid input.
bool Text_SetCharAndColorAtXY(uint8_t x, uint8_t y, uint8_t the_char, uint8_t fore_color, uint8_t back_color)
{
uint8_t* the_write_loc;
uint8_t the_attribute_value;
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
the_write_loc = Text_GetMemLocForXY(x, y);
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
*the_write_loc = the_attribute_value;
Sys_RestoreIOPage();
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
*the_write_loc = the_char;
Sys_RestoreIOPage();
last_x = x;
last_y = y;
return true;
}
// //! Set a char at a y*80+x screen index point.
// bool Text_SetCharAtMemLoc(uint16_t the_write_loc, uint8_t the_char)
// {
// the_write_loc += SCREEN_TEXT_MEMORY_LOC;
//
// *(uint8_t*)the_write_loc = the_char;
//
// return true;
// }
// copy n-bytes into display memory, at the X/Y position specified
bool Text_DrawCharsAtXY(uint8_t x, uint8_t y, uint8_t* the_buffer, uint16_t the_len)
{
uint8_t* the_char_loc;
uint16_t i;
// set up char and attribute memory initial loc
the_char_loc = Text_GetMemLocForXY(x, y);
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
// draw the string
for (i = 0; i < the_len; i++)
{
*the_char_loc++ = the_buffer[i];
}
Sys_RestoreIOPage();
last_x = x + i;
last_y = y;
return true;
}
// **** FONT RELATED *****
// //! replace the current font data with the data at the passed memory buffer
// //! @param the_screen: valid pointer to the target screen to operate on
// //! @param new_font_data: Pointer to 2K (256 characters x 8 lines/bytes each) of font data. Each byte represents one line of an 8x8 font glyph.
// //! @return Returns false on any error/invalid input.
// bool Text_UpdateFontData(char* new_font_data, bool for_primary_font)
// {
// uint8_t* target_font_addr;
//
// if (new_font_data == NULL)
// {
// LOG_ERR(("%s %d: passed font data was NULL", __func__, __LINE__));
// return false;
// }
//
// if (for_primary_font)
// {
// target_font_addr = (uint8_t*)FONT_MEMORY_BANK0;
// }
// else
// {
// target_font_addr = (uint8_t*)FONT_MEMORY_BANK1;
// }
//
// Sys_SwapIOPage(VICKY_IO_PAGE_FONT_AND_LUTS);
//
// memcpy(target_font_addr, new_font_data, (8*256));
//
// Sys_RestoreIOPage();
//
// return true;
// }
// **** Get char/attr functions *****
// **** Drawing functions *****
//! Draws a horizontal line from specified coords, for n characters, using the specified char and/or attribute
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_line_len: The total length of the line, in characters, including the start and end character.
//! @param the_char: the character to be used when drawing
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param the_draw_choice: controls the scope of the action, and is one of CHAR_ONLY, ATTR_ONLY, or CHAR_AND_ATTR. See the text_draw_choice enum.
//! @return Returns false on any error/invalid input.
void Text_DrawHLine(uint8_t x, uint8_t y, uint8_t the_line_len, uint8_t the_char, uint8_t fore_color, uint8_t back_color, uint8_t the_draw_choice)
{
uint8_t the_attribute_value;
// LOGIC:
// an H line is just a box with 1 row, so we can re-use Text_FillMemoryBox(Both)(). These routines use memset, so are quicker than for loops.
if (the_draw_choice == CHAR_ONLY)
{
Text_FillMemoryBox(x, y, the_line_len, 0, SCREEN_FOR_TEXT_CHAR, the_char);
}
else
{
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
if (the_draw_choice == ATTR_ONLY)
{
Text_FillMemoryBox(x, y, the_line_len, 0, SCREEN_FOR_TEXT_ATTR, the_attribute_value);
}
else
{
Text_FillMemoryBoxBoth(x, y, the_line_len, 0, the_char, the_attribute_value);
}
}
last_x = x + the_line_len;
last_y = y;
}
//! Draws a vertical line from specified coords, for n characters, using the specified char and/or attribute
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_line_len: The total length of the line, in characters, including the start and end character.
//! @param the_char: the character to be used when drawing
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param the_draw_choice: controls the scope of the action, and is one of CHAR_ONLY, ATTR_ONLY, or CHAR_AND_ATTR. See the text_draw_choice enum.
//! @return Returns false on any error/invalid input.
void Text_DrawVLine(uint8_t x, uint8_t y, uint8_t the_line_len, uint8_t the_char, uint8_t fore_color, uint8_t back_color, uint8_t the_draw_choice)
{
uint8_t dy;
switch (the_draw_choice)
{
case CHAR_ONLY:
for (dy = 0; dy < the_line_len; dy++)
{
Text_SetCharAtXY(x, y + dy, the_char);
}
break;
case ATTR_ONLY:
for (dy = 0; dy < the_line_len; dy++)
{
Text_SetColorAtXY(x, y + dy, fore_color, back_color);
}
break;
case CHAR_AND_ATTR:
default:
for (dy = 0; dy < the_line_len; dy++)
{
Text_SetCharAndColorAtXY(x, y + dy, the_char, fore_color, back_color);
}
break;
}
last_x = x;
last_y = y + dy;
}
//! Draws a box based on 2 sets of coords, using the predetermined line and corner "graphics", and the passed colors
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x1: the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y1: the uppermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param x2: the rightmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y2: the lowermost vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return Returns false on any error/invalid input.
void Text_DrawBoxCoordsFancy(uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t fore_color, uint8_t back_color)
{
uint8_t dy;
uint8_t dx;
//DEBUG_OUT(("%s %d: %u, %u x %u, %u", __func__, __LINE__, x1, y1, x2, y2));
// // add 1 to H line len, because dx becomes width, and if width = 0, then memset gets 0, and nothing happens.
// dy can be 0 and you still get at least one row done.
// but, for this, because of how we draw H line, do NOT add 1 to x1. see "x1+1" below...
dx = x2 - x1 + 0;
dy = y2 - y1 + 0;
// draw all lines one char shorter on each end so that we don't overdraw when we do corners
Text_DrawHLine(x1+1, y1, dx, SC_HLINE, fore_color, back_color, CHAR_AND_ATTR);
Text_DrawHLine(x1+1, y2, dx, SC_HLINE, fore_color, back_color, CHAR_AND_ATTR);
Text_DrawVLine(x2, y1+1, dy, SC_VLINE, fore_color, back_color, CHAR_AND_ATTR);
Text_DrawVLine(x1, y1+1, dy, SC_VLINE, fore_color, back_color, CHAR_AND_ATTR);
// draw the 4 corners with dedicated corner pieces
Text_SetCharAndColorAtXY(x1, y1, SC_ULCORNER, fore_color, back_color);
Text_SetCharAndColorAtXY(x2, y1, SC_URCORNER, fore_color, back_color);
Text_SetCharAndColorAtXY(x2, y2, SC_LRCORNER, fore_color, back_color);
Text_SetCharAndColorAtXY(x1, y2, SC_LLCORNER, fore_color, back_color);
last_x = x2;
last_y = y2;
}
// **** Draw string functions *****
//! Draw a string at a specified x, y coord, also setting the color attributes.
//! If it is too long to display on the line it started, it will be truncated at the right edge of the screen.
//! No word wrap is performed.
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the starting horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the starting vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param the_string: the null-terminated string to be drawn
//! @param fore_color: Index to the desired foreground color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @param back_color: Index to the desired background color (0-15). The predefined macro constants may be used (COLOR_DK_RED, etc.), but be aware that the colors are not fixed, and may not correspond to the names if the LUT in RAM has been modified.
//! @return Returns false on any error/invalid input.
bool Text_DrawStringAtXY(uint8_t x, uint8_t y, char* the_string, uint8_t fore_color, uint8_t back_color)
{
uint8_t* the_char_loc;
uint8_t* the_attr_loc;
uint8_t the_attribute_value;
uint8_t i;
uint8_t max_col;
uint8_t draw_len;
draw_len = strlen(the_string); // can't be wider than the screen anyway
max_col = SCREEN_NUM_COLS - 1;
if (x + draw_len > max_col)
{
draw_len = (max_col - x) + 1;
}
//DEBUG_OUT(("%s %d: draw_len=%i, max_col=%i, x=%i", __func__, __LINE__, draw_len, max_col, x));
//printf("%s %d: draw_len=%i, max_col=%i, x=%i \n", __func__, __LINE__, draw_len, max_col, x);
// calculate attribute value from passed fore and back colors
// LOGIC: text mode only supports 16 colors. lower 4 bits are back, upper 4 bits are foreground
the_attribute_value = ((fore_color << 4) | back_color);
// set up char and attribute memory initial loc
the_attr_loc = the_char_loc = Text_GetMemLocForXY(x, y);
//printf("%s %d: the_char_loc=%p, *charloc=%u \n", __func__, __LINE__, the_char_loc, *the_char_loc);
//printf("%s %d: string=%s \n", __func__, __LINE__, the_string);
// draw the string
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
for (i = 0; i < draw_len; i++)
{
*the_char_loc++ = the_string[i];
}
Sys_RestoreIOPage();
// draw the attributes
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
memset(the_attr_loc, the_attribute_value, draw_len);
Sys_RestoreIOPage();
last_x = x + i;
last_y = y;
return true;
}
// **** Plotting functions ****
//! Calculate the VRAM location of the specified coordinate
//! @param the_screen: valid pointer to the target screen to operate on
//! @param x: the horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y: the vertical position, between 0 and the screen's text_rows_vis_ - 1
//! @param for_attr: true to work with attribute data, false to work character data. Recommend using SCREEN_FOR_TEXT_ATTR/SCREEN_FOR_TEXT_CHAR.
uint8_t* Text_GetMemLocForXY(uint8_t x, uint8_t y)
{
uint8_t* the_write_loc;
uint16_t initial_offset;
// LOGIC:
// For plotting the VRAM, A2560 uses the full width, regardless of borders.
// So even if only 72 are showing, the screen is arranged from 0-71 for row 1, then 80-151 for row 2, etc.
initial_offset = (SCREEN_NUM_COLS * y) + x;
the_write_loc = (uint8_t*)SCREEN_TEXT_MEMORY_LOC + initial_offset;
//DEBUG_OUT(("%s %d: screen=%i, x=%i, y=%i, for-attr=%i, calc=%i, loc=%p", __func__, __LINE__, (int16_t)the_screen_id, x, y, for_attr, (the_screen->text_mem_cols_ * y) + x, the_write_loc));
last_x = x;
last_y = y;
return the_write_loc;
}
// **** "Text Window" Functions ****