struct / union in initializer, RFE #901.
[sdcc.git] / sdcc / support / regression / tests / bug-3322.c
blob3dbb2849bf97c77a1a6ff8a64b72be5e58afe515
1 /*
2 bug-3322.c
3 A z80 code generation (overwriting a value in iy during an assignment) bug found by z88dk
4 */
6 #include <testfwk.h>
8 #define PIECE_IS_NEXT 255
10 #define STARTBOARD_X 15
11 #define STARTBOARD_Y 2
13 #define POS_NEXT_X 3
14 #define POS_NEXT_Y 1
16 #define NEXT_X (POS_NEXT_X+2)
17 #define NEXT_Y (POS_NEXT_Y+3)
19 #define EMPTY_GR_CHAR 32
20 #define EMPTY_GR_COLOR 0
21 #define FONTS 0
23 typedef unsigned char byte;
25 // tetromino sprite
26 typedef struct {
27 int x; // signed integers to allow negative board positions
28 int y; // -1 means piece is preview ("next")
29 byte piece;
30 byte angle;
31 } sprite;
33 #define gr4_tile(x,y,tile,color,tileset) _gr4_tile_x = (x); _gr4_tile_y = (y); _gr4_tile_tile = (tile); _gr4_tile_color = (color); _gr4_tile_tileset = (tileset); _gr4_tile()
35 byte _gr4_tile_x;
36 byte _gr4_tile_y;
37 byte _gr4_tile_tile;
38 byte _gr4_tile_color;
39 byte *_gr4_tile_tileset;
41 byte *_gr4_tile_source;
42 byte *_gr4_tile_dest;
44 void _gr4_tile() {
47 // offset of a single tile composing a piece
48 typedef struct {
49 byte offset_x;
50 byte offset_y;
51 } tile_offset;
53 tile_offset *get_piece_offsets(byte piece, byte angle);
55 tile_offset pieces_XY[4] = {
56 {+1,+1} , {+2,+1} , {+3,+1} , {+1,+2} ,
59 void SLOT1_VIDEO_START() {
62 void SLOT1_END() {
65 // given a piece number and an angle returns the 4 byte "offsets" of the piece
66 tile_offset *get_piece_offsets(byte piece, byte angle) {
67 return &pieces_XY[(piece*sizeof(tile_offset)*4*2)+angle*4];
70 void gr_erasepiece(sprite *p);
72 // erase piece from the screen
73 void gr_erasepiece(sprite *p) {
74 tile_offset *data = get_piece_offsets(p->piece, p->angle); // bug if this declaration is here
76 int px = p->x;
77 int py = p->y;
79 // are we erasing the "next" piece ?
80 if(py==PIECE_IS_NEXT) {
81 px = NEXT_X;
82 py = NEXT_Y;
84 else {
85 px += STARTBOARD_X;
86 py += STARTBOARD_Y;
89 // tile_offset *data = get_piece_offsets(p->piece, p->angle); // no bug if this declaration is here
91 SLOT1_VIDEO_START();
92 for(byte t=0; t<4; t++) {
93 int x = px + data->offset_x;
94 int y = py + data->offset_y;
95 data++;
96 gr4_tile((byte)x,(byte)y,EMPTY_GR_CHAR,EMPTY_GR_COLOR,FONTS);
98 SLOT1_END();
101 void testBug(void)
103 sprite s;
105 s.x = 0;
106 s.y = 0;
107 s.piece = 0;
108 s.angle = 0;
109 gr_erasepiece(&s);
110 ASSERT(_gr4_tile_x == s.x + STARTBOARD_X + pieces_XY[3].offset_x);
111 ASSERT(_gr4_tile_y == s.y + STARTBOARD_Y + pieces_XY[3].offset_y);