-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtext.c
2171 lines (1765 loc) · 76.9 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 "debug.h"
#include "general.h"
#include "keyboard.h"
#include "text.h"
#include "sys.h"
// C includes
#include <stdbool.h>
#include <stdarg.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// F256 includes
#include "f256.h"
/*****************************************************************************/
/* Definitions */
/*****************************************************************************/
// hide __fastcall_ from everything but CC65 (to squash some warnings in LSP/BBEdit)
#ifndef __CC65__
#define __fastcall__
#endif
/*****************************************************************************/
/* Enumerations */
/*****************************************************************************/
/*****************************************************************************/
/* File-scoped Variables */
/*****************************************************************************/
/*****************************************************************************/
/* Global Variables */
/*****************************************************************************/
extern System* global_system;
extern uint8_t* zp_ptr;
extern uint8_t* zp_vram_ptr;
extern uint16_t zp_len;
extern uint8_t zp_x;
extern uint8_t zp_y;
extern uint8_t zp_char;
#pragma zpsym ("zp_ptr");
#pragma zpsym ("zp_vram_ptr");
#pragma zpsym ("zp_len");
#pragma zpsym ("zp_x");
#pragma zpsym ("zp_y");
#pragma zpsym ("zp_char");
/*****************************************************************************/
/* Private Function Prototypes */
/*****************************************************************************/
// (in text_ml.asm)
//! Calculate the VRAM location of the specified coordinate, update VICKY cursor pos
//! Set zp_x, zp_y before calling
void __fastcall__ Text_SetMemLocForXY(void);
//! Validate the coordinates are within the bounds of the specified screen.
//! @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 coords, attribute value before passing!
//! @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 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 PARAM_FOR_TEXT_ATTR/PARAM_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.
//! @param for_attr - true to work with attribute data, false to work character data. Recommend using PARAM_FOR_TEXT_ATTR/PARAM_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)
{
uint8_t* the_write_loc;
// LOGIC:
// On F256-classic, the write locs are same for char and attr memory, difference is IO page 2 or 3
// On F256-extended, the write locs are different for char and attr memory, as E loads use flat memory map
if (for_attr)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
}
else
{
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
}
the_write_loc = (uint8_t*)SCREEN_TEXT_MEMORY_LOC;
memset(the_write_loc, the_fill, SCREEN_TOTAL_BYTES);
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 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 max_row;
// set up initial loc
Text_SetXY(x,y);
// LOGIC:
// On F256-classic, the write locs are same for char and attr memory, difference is IO page 2 or 3
// On F256-extended, the write locs are different for char and attr memory, as E loads use flat memory map
max_row = (y + height) - 1; // we are passing '1' for a single h row
for (; y <= max_row; y++)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
memset(zp_vram_ptr, the_attribute_value, width);
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
memset(zp_vram_ptr, the_char, width);
zp_vram_ptr += SCREEN_NUM_COLS;
}
Sys_RestoreIOPage();
// reset current vram loc to match x,y
Text_SetXY(x,y);
return true;
}
//! Fill character OR attribute memory for a specific box area
//! calling function must validate coords, attribute value before passing!
//! @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 PARAM_FOR_TEXT_ATTR/PARAM_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 max_row;
// set up initial loc
Text_SetXY(x,y);
// LOGIC:
// On F256-classic, the write locs are same for char and attr memory, difference is IO page 2 or 3
// On F256-extended, the write locs are different for char and attr memory, as E loads use flat memory map
if (for_attr)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
}
else
{
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
}
max_row = (y + height) - 1; // we are passing '1' for a single h row
for (; y <= max_row; y++)
{
memset(zp_vram_ptr, the_fill, width);
zp_vram_ptr += SCREEN_NUM_COLS;
}
Sys_RestoreIOPage();
// reset current vram loc to match x,y
Text_SetXY(x,y);
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 ****
// //! Copies characters and attributes from the left, to the right, for the passed length, backfilling with the char and attr passed
// //! Shift never extends beyond the current row of text.
// //! @param working_buffer - valid pointer to a block of memory at least SCREEN_NUM_COLS in size, to act as a temporary line buffer for the operation.
// //! @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 shift_count - the number of character positions text will be shifted. eg, '1' will shift everything to the right of x by 1 character.
// //! @param backfill_char - the character to place in the space freed up by copy. eg, if you shift 10 chars at positions 60-69 to 70-79, this char will be used to fill the slots from 60-69.
// //! @param backfill_fore_color - foreground color that will be applied to the space opened up by the copy
// //! @param backfill_back_color - background color that will be applied to the space opened up by the copy
// //! @return Returns false on any error/invalid input.
// bool Text_ShiftTextAndAttrRight(uint8_t* working_buffer, uint8_t x, uint8_t y, uint8_t shift_count, uint8_t backfill_char, uint8_t backfill_fore_color, uint8_t backfill_back_color)
// {
// uint8_t* vram_to_loc;
// uint8_t* vram_from_loc;
// int16_t initial_offset;
// uint8_t the_length;
// 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 = ((backfill_fore_color << 4) | backfill_back_color);
//
// // LOGIC:
// // On F256-classic, the write locs are same for char and attr memory, difference is IO page 2 or 3
// // On F256-extended, the write locs are different for char and attr memory, as E loads use flat memory map
//
// // check for valid inputs to ensure we are only adjusting 1 valid line worth of text
// if (y > SCREEN_LAST_ROW)
// {
// return false;
// }
// if (x > SCREEN_LAST_COL) // allow position 79 (eg) to be shifted off the screen
// {
// return false;
// }
// if ((x + shift_count) > SCREEN_LAST_COL)
// {
// // can't shift more right than the end of the row
// shift_count = (SCREEN_LAST_COL - x) + 1;
// }
//
// // get initial read/write locs - copy from right most character, not left-most.
// the_length = (SCREEN_NUM_COLS - x) - shift_count; // if x=70, and want to shift 5 chars to right, len can't be 10, len must be 5 or we'll overwrite next line
// initial_offset = (SCREEN_NUM_COLS * y) + x;
//
// // copy text
// vram_from_loc = (uint8_t*)VICKY_TEXT_CHAR_RAM + initial_offset;
// vram_to_loc = vram_from_loc + shift_count;
// memcpy(working_buffer, vram_from_loc, the_length);
// memcpy(vram_to_loc, working_buffer, the_length);
// // backfill text
// memset(vram_from_loc, backfill_char, shift_count);
//
//
// // copy attr
// vram_from_loc = (uint8_t*)VICKY_TEXT_ATTR_RAM + initial_offset;
// vram_to_loc = vram_from_loc + shift_count;
// memcpy(working_buffer, vram_from_loc, the_length);
// memcpy(vram_to_loc, working_buffer, the_length);
// // backfill attr
// memset(vram_from_loc, the_attribute_value, shift_count);
//
// return true;
// }
// //! Copies characters and attributes from the right, to the left, for the passed length, backfilling with the char and attr passed
// //! Shift never extends beyond the current row of text.
// //! @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 shift_count - the number of character positions text will be shifted. eg, '1' will shift everything to the left of x by 1 character.
// //! @param backfill_char - the character to place in the space freed up by copy. eg, if you shift 10 chars at positions 70-79 to 60-69, this char will be used to fill the slots from 70-79.
// //! @param backfill_fore_color - foreground color that will be applied to the space opened up by the copy
// //! @param backfill_back_color - background color that will be applied to the space opened up by the copy
// //! @return Returns false on any error/invalid input.
// bool Text_ShiftTextAndAttrLeft(uint8_t x, uint8_t y, uint8_t shift_count, uint8_t backfill_char, uint8_t backfill_fore_color, uint8_t backfill_back_color)
// {
// uint8_t* vram_to_loc;
// uint8_t* vram_from_loc;
// int16_t initial_offset;
// uint8_t the_length;
// 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 = ((backfill_fore_color << 4) | backfill_back_color);
//
// // LOGIC:
// // On F256jr, the write len and write locs are same for char and attr memory, difference is IO page 2 or 3
//
// // check for valid inputs to ensure we are only adjusting 1 valid line worth of text
// if (y > SCREEN_LAST_ROW)
// {
// return false;
// }
// if (x == 0 || x > SCREEN_LAST_COL)
// {
// return false;
// }
// if (x < shift_count)
// {
// // can't shift more left than the start of the row
// shift_count = x;
// }
//
// // get initial read/write locs
// the_length = SCREEN_NUM_COLS - x;
// initial_offset = (SCREEN_NUM_COLS * y) + x;
//
// // copy text
// vram_from_loc = (uint8_t*)VICKY_TEXT_CHAR_RAM + initial_offset;
// vram_to_loc = vram_from_loc - shift_count;
// memcpy(vram_to_loc, vram_from_loc, the_length);
// // backfill text
// vram_from_loc += (the_length - 1);
// memset(vram_from_loc, backfill_char, shift_count);
//
// // copy attr
// vram_from_loc = (uint8_t*)VICKY_TEXT_ATTR_RAM + initial_offset;
// vram_to_loc = vram_from_loc - shift_count;
// memcpy(vram_to_loc, vram_from_loc, the_length);
// // backfill attr
// vram_from_loc += (the_length - 1);
// memset(vram_from_loc, the_attribute_value, shift_count);
//
// return true;
// }
// //! scrolls the text and attribute memory up ONE row.
// //! e.g, row 0 is lost. row 1 becomes row 0, row 49 becomes row 48, row 49 is cleared.
// //! @param y1 - the first row to scroll up
// //! @param y2 - the last row to scroll up
// //! @return Returns false on any error/invalid input.
// bool Text_ScrollTextAndAttrRowsUp(uint8_t y1, uint8_t y2)
// {
// uint8_t* vram_to_loc;
// uint8_t* vram_from_loc;
// int16_t initial_offset;
// uint8_t num_rows;
// uint8_t i;
//
// // LOGIC:
// // On F256-classic, the write locs are same for char and attr memory, difference is IO page 2 or 3
// // On F256-extended, the write locs are different for char and attr memory, as E loads use flat memory map
//
// // adjust the x, y, x2, y2, so that we are never trying to copy out of the physical screen box
// if (y1 < 1)
// {
// y1 = 1; // can't scroll row 0 anywhere useful.
// }
// else if (y1 > SCREEN_LAST_ROW)
// {
// y1 = SCREEN_LAST_ROW;
// }
// if (y2 < y1)
// {
// y2 = y1; // ok to scroll 1 row, so this is compromise for bad data.
// }
// else if (y2 > SCREEN_LAST_ROW)
// {
// y2 = SCREEN_LAST_ROW;
// }
//
// // get initial read/write locs
// initial_offset = (SCREEN_NUM_COLS * y1);
// num_rows = y2 - y1 + 1;
//
// vram_from_loc = (uint8_t*)SCREEN_TEXT_MEMORY_LOC + initial_offset;
// vram_to_loc = vram_from_loc - SCREEN_NUM_COLS;
//
// for (i = 0; i < num_rows; i++)
// {
// Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
// memcpy(vram_to_loc, vram_from_loc, SCREEN_NUM_COLS);
// Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
// memcpy(vram_to_loc, vram_from_loc, SCREEN_NUM_COLS);
//
// vram_to_loc = vram_from_loc;
// vram_from_loc += SCREEN_NUM_COLS;
// }
//
// Sys_RestoreIOPage();
//
// return true;
// }
// //! scrolls the text and attribute memory down ONE row.
// //! e.g, row 59 is lost. row 58 becomes row 59, row 0 becomes row 1, row 0 is cleared.
// //! @param y1 - the first row to scroll down
// //! @param y2 - the last row to scroll down. y2+1 is overwritten, y2+2 and beyond are left as is.
// //! @return Returns false on any error/invalid input.
// bool Text_ScrollTextAndAttrRowsDown(uint8_t y1, uint8_t y2)
// {
// uint8_t* vram_to_loc;
// uint8_t* vram_from_loc;
// int16_t initial_offset;
// uint8_t num_rows;
// uint8_t i;
//
// // 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 (y2 == SCREEN_LAST_ROW)
// {
// y2 = SCREEN_LAST_ROW - 1; // can't scroll row 59 anywhere useful.
// }
// else if (y2 < 1)
// {
// y2 = 1;
// }
// if (y2 < y1)
// {
// y2 = y1; // ok to scroll 1 row, so this is compromise for bad data.
// }
//
// // get initial read/write locs
// initial_offset = (SCREEN_NUM_COLS * y2);
// num_rows = y2 - y1 + 1;
//
// vram_from_loc = (uint8_t*)VICKY_TEXT_CHAR_RAM + initial_offset;
// vram_to_loc = vram_from_loc + SCREEN_NUM_COLS;
//
// for (i = 0; i < num_rows; i++)
// {
// memcpy(vram_to_loc, vram_from_loc, SCREEN_NUM_COLS);
//
// vram_to_loc = vram_from_loc;
// vram_from_loc -= SCREEN_NUM_COLS;
// }
//
// vram_from_loc = (uint8_t*)VICKY_TEXT_ATTR_RAM + initial_offset;
// vram_to_loc = vram_from_loc + SCREEN_NUM_COLS;
//
// for (i = 0; i < num_rows; i++)
// {
// memcpy(vram_to_loc, vram_from_loc, SCREEN_NUM_COLS);
//
// vram_to_loc = vram_from_loc;
// vram_from_loc -= SCREEN_NUM_COLS;
// }
//
// return true;
// }
#if defined DO_NOT_HIDE_ME_BRO
//! Copy a linear run 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, and do not have a rectangular area
//! of the screen to copy to/from, but instead want to copy a single linear stream to/from a particular cursor position.
//! @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 screen memory. This will be read from first byte to last byte, without skipping. e.g., if you want to copy a 227 characters of text from the middle of the screen to this buffer, the buffer must be 227 bytes in length, and data will be written contiguously to it.
//! @param x - the leftmost horizontal position, between 0 and the screen's text_cols_vis_ - 1
//! @param y - the uppermost 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 PARAM_COPY_TO_SCREEN/PARAM_COPY_FROM_SCREEN.
//! @param for_attr - true to work with attribute data, false to work character data. Recommend using PARAM_FOR_TEXT_ATTR/PARAM_FOR_TEXT_CHAR.
//! @return Returns false on any error/invalid input.
bool Text_CopyMemLinearBuffer(uint8_t* the_buffer, uint8_t x, uint8_t y, uint16_t the_len, bool to_screen, bool for_attr)
{
uint8_t* the_vram_loc;
uint8_t* the_buffer_loc;
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 to ensure we have legitimate positions
if (x > SCREEN_LAST_COL)
{
x = SCREEN_LAST_COL;
}
if (y > SCREEN_LAST_ROW)
{
y = SCREEN_LAST_ROW;
}
// get initial read/write locs
initial_offset = (SCREEN_NUM_COLS * y) + x;
// prevent writing past end of screen memory
if (initial_offset + the_len > 2000)
{
the_len = 2000 - initial_offset;
}
// LOGIC:
// On F256jr/k, the write locs are same for char and attr memory, difference is IO page 2 or 3
// On F256k2, the write locs are different for char and attr memory, as k2 uses flat memory map
if (for_attr)
{
the_vram_loc = (uint8_t*)(VICKY_TEXT_ATTR_RAM + initial_offset);
}
else
{
the_vram_loc = (uint8_t*)(VICKY_TEXT_CHAR_RAM + initial_offset);
}
the_buffer_loc = the_buffer;
// 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));
if (to_screen)
{
memcpy(the_vram_loc, the_buffer_loc, the_len);
}
else
{
memcpy(the_buffer_loc, the_vram_loc, the_len);
}
return true;
}
#endif
//! 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_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 PARAM_COPY_TO_SCREEN/PARAM_COPY_FROM_SCREEN.
//! @param for_attr - true to work with attribute data, false to work character data. Recommend using PARAM_FOR_TEXT_ATTR/PARAM_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* orig_vram_loc;
uint8_t* the_buffer_loc;
uint8_t the_write_len;
// 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;
}
// set up initial loc
orig_vram_loc = zp_vram_ptr;
Text_SetXY(x1,y1);
// LOGIC:
// On F256-classic, the write locs are same for char and attr memory, difference is IO page 2 or 3
// On F256-extended, the write locs are different for char and attr memory, as E loads use flat memory map
if (for_attr)
{
Sys_SwapIOPage(VICKY_IO_PAGE_ATTR_MEM);
}
else
{
Sys_SwapIOPage(VICKY_IO_PAGE_CHAR_MEM);
}
the_buffer_loc = the_buffer;
the_write_len = x2 - x1 + 1;
// 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(zp_vram_ptr, the_buffer_loc, the_write_len);
}
else
{
memcpy(the_buffer_loc, zp_vram_ptr, the_write_len);
}
the_buffer_loc += the_write_len;
zp_vram_ptr += SCREEN_NUM_COLS;
}
Sys_RestoreIOPage();
// restore screen addr
zp_vram_ptr = orig_vram_loc;
return true;
}
#if defined DO_NOT_HIDE_ME_BRO
//! 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_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 PARAM_COPY_TO_SCREEN/PARAM_COPY_FROM_SCREEN.
//! @param for_attr - true to work with attribute data, false to work character data. Recommend using PARAM_FOR_TEXT_ATTR/PARAM_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;
}
#endif
// **** 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(PARAM_FOR_TEXT_CHAR, ' ');
Text_FillMemory(PARAM_FOR_TEXT_ATTR, the_attribute_value);
}
//! Fill character and attribute memory for a specific box area
//! @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.
// same for dy, as we account for that in the next function called
dx = x2 - x1 + 1;
dy = y2 - y1 + 1;
// 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 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.
// // same for dy, as we account for that in the next function called
// dx = x2 - x1 + 1;
// dy = y2 - y1 + 1;
//
// return Text_FillMemoryBox(x1, y1, dx, dy, PARAM_FOR_TEXT_CHAR, the_char);
// }
//! Fill attribute memory for a specific box area
//! @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.
// same for dy, as we account for that in the next function called
dx = x2 - x1 + 1;
dy = y2 - y1 + 1;
// 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, PARAM_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 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_col;
// uint8_t skip_len;
// uint8_t back_nibble;
// uint8_t fore_nibble;
// uint8_t* the_write_loc;
//
// // get initial read/write loc
// Text_SetXY(x1,y1);
// the_write_loc = zp_vram_ptr;
//
// // 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 = R8(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();
//
// // note: for this function, we will not update the next write VRAM address to the point after the lower right corner.
//
// return true;
// }
// **** Cursor positioning functions *****
//! Move cursor to the specified x, y coord
//! @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
void Text_SetXY(uint8_t x, uint8_t y)
{
// uint16_t initial_offset;
//
// // LOGIC:
// // For plotting the VRAM, VICKY 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.
// // For F256K/JR non-flat memory loads, char ram and attr ram are at same address, and only difference is which I/O bank is being used
// // For F256K/JR/K2 with flat memory loads, char and ram are at different addresses
// // the file-scoped current x/y are always set when this function is called, regardless if for attr or char
// // the file-scoped current memory address is also always set, but only ever points to the char memory, not attr memory.
//
// initial_offset = (SCREEN_NUM_COLS * y) + x;
//
// // save the new current address, x, y position, and also tell VICKY where the cursor should be
// zp_vram_ptr = (uint8_t*)SCREEN_TEXT_MEMORY_LOC + initial_offset;
// zp_x = x;
// zp_y = y;
//
// Sys_SwapIOPage(VICKY_IO_PAGE_REGISTERS);
// R8(VICKY_TEXT_X_POS) = zp_x;
// R8(VICKY_TEXT_Y_POS) = zp_y;
// Sys_RestoreIOPage();
zp_x = x;
zp_y = y;
Text_SetMemLocForXY();
}
#if defined DO_NOT_HIDE_ME_BRO
//! Return the current cursor location's X coordinate
//! @return the horizontal position, between 0 and the screen's text_cols_vis_ - 1
uint8_t Text_GetX(void)
{
return zp_x;
}
#endif
#if defined DO_NOT_HIDE_ME_BRO
//! Return the current cursor location's Y coordinate
//! @return the vertical position, between 0 and the screen's text_rows_vis_ - 1
uint8_t Text_GetY(void)
{
return zp_y;
}
#endif
// **** Set char/attr functions *****
// NOTE: all functions from here lower that pass an x/y will update the zp_x/zp_y parameters.
//! Set a char at a specified x, y coord
//! @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)
{
Text_SetXY(x, y);
Text_SetChar(the_char);
return true;
}
#if defined DO_NOT_HIDE_ME_BRO
//! Set the attribute value at a specified x, y coord
//! @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)
{
Text_SetXY(x, y);
Text_SetAttr(the_attribute_value);
return true;
}
#endif
//! Set the attribute value at a specified x, y coord based on the foreground and background colors passed
//! @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)
{