1 /*************************************************************************/
2 /*                                                                       */
3 /*                    Ram editor source file                             */
4 /*                                                                       */
5 /* A few functions to edit/view RAM as asked by Dave Shadoff ;)          */
6 /* Note that cheating functions can also be helpful                      */
7 /*                                                                       */
8 /*************************************************************************/
9 
10 #include "edit_ram.h"
11 
12 #define  NB_BYTE_LINE   8
13 // Number of byte displayed on each line
14 
15 #define  NB_LINE        20
16 // Number of displayed lines
17 
18 /*
19 static char out;
20 // To know whether we got to quit
21 
22 static int frame_up, frame_down;
23 // The currently displayed "frame" in RAM
24 
25 static unsigned short selected_byte;
26 // The current offset
27 */
28 
29 /*****************************************************************************
30 
31     Function:  change_value
32 
33     Description: change the value at selected_byte
34     Parameters: none, use global selected_byte
35     Return:nothing, but may modify the RAM
36 
37 *****************************************************************************
38 void change_value()
39 {
40  unsigned X=(((selected_byte&0x04)/4)*2+6+3*(selected_byte&0x07))*8+blit_x,Y=(selected_byte-frame_up)/NB_BYTE_LINE*10+blit_y-1;
41  char out=0,index=0;
42  char value[3]="\0\0";
43  int ch;
44 
45  do {
46  rectfill(screen,X,Y,X+16,Y+9,-15);
47  textout(screen,font,value,X,Y+1,-1);
48  ch=osd_readkey();
49 
50  // first switch by scancode
51  switch (ch>>8)
52    {
53     case KEY_ESC:
54                         out=1;
55          break;
56     case KEY_ENTER:
57                         RAM[selected_byte]=cvtnum(value);
58                         out=1;
59                         break;
60     case KEY_BACKSPACE:
61                         if (index)
62                           value[--index]=0;
63          break;
64    }
65 
66  // Now by ascii code
67  switch (ch&0xff)
68    {
69     case '0' ... '9':
70     case 'a' ... 'f':
71     case 'A' ... 'F':
72                         if (index<2)
73                           value[index++]=toupper(ch&0xff);
74          break;
75    }
76  } while (!out);
77 
78  return ;
79  }
80 
81 */
82 
83 /*****************************************************************************
84 
85     Function:  ram_key
86 
87     Description: handle the keyboard in edit_ram
88     Parameters: none
89     Return: nothing
90 
91 *****************************************************************************/
92 void
ram_key()93 ram_key ()
94 {
95 
96   /* TODO: deallegroize here too */
97 #ifdef ALLEGRO
98   int ch = osd_readkey ();
99 
100   switch (ch >> 8)
101     {
102       // The two first are a bit special
103     case KEY_HOME:
104       selected_byte = 0;
105       frame_up = 0;
106       frame_down = NB_BYTE_LINE * NB_LINE;
107       return;
108     case KEY_END:
109       selected_byte = 0x7FFF;
110       frame_down = 0x8000;
111       frame_up = frame_down - NB_BYTE_LINE * NB_LINE;
112       return;
113     case KEY_PGUP:
114       if (selected_byte >= NB_BYTE_LINE * NB_LINE)
115 	selected_byte -= NB_BYTE_LINE * NB_LINE;
116       break;
117     case KEY_PGDN:
118       if (selected_byte <= 0x7FFF - NB_BYTE_LINE * NB_LINE);
119       selected_byte += NB_BYTE_LINE * NB_LINE;
120       break;
121     case KEY_UP:
122       if (selected_byte >= NB_BYTE_LINE)
123 	selected_byte -= NB_BYTE_LINE;
124       break;
125     case KEY_DOWN:
126       if (selected_byte <= 0x7FFF - NB_BYTE_LINE)
127 	selected_byte += NB_BYTE_LINE;
128       break;
129     case KEY_RIGHT:
130       if (selected_byte < 0x7FFF)
131 	selected_byte++;
132       break;
133     case KEY_LEFT:
134       if (selected_byte)
135 	selected_byte--;
136       break;
137     case KEY_SPACE:
138       {
139 	UInt32 dummy = RAM[selected_byte];
140 
141 	change_value (
142 		      (((selected_byte & 0x04) / 4) * 2 + 6 +
143 		       3 * (selected_byte & 0x07)) * 8 + blit_x,
144 		      (selected_byte - frame_up) / NB_BYTE_LINE * 10 +
145 		      blit_y - 1, 2, &dummy);
146 
147 	RAM[selected_byte] = (UChar) dummy;
148 
149       }
150       break;
151     case KEY_F12:
152     case KEY_ESC:
153       out = 1;
154       break;
155     }
156 
157   // Now ajust the frame
158   if ((selected_byte < 3 * NB_BYTE_LINE) ||
159       (selected_byte > 0x7FFF - 3 * NB_BYTE_LINE))
160     return;
161 
162   if (selected_byte >= frame_down - 3 * NB_BYTE_LINE)
163     {
164       frame_down = min (0x8000, (selected_byte + 3 * NB_BYTE_LINE) & 0x7FF8);
165       frame_up = frame_down - NB_BYTE_LINE * NB_LINE;
166     }
167 
168   if (selected_byte < frame_up + 3 * NB_BYTE_LINE)
169     {
170       frame_up = max (0, (int) ((selected_byte - 3 * NB_BYTE_LINE) & 0x7FF8));
171       frame_down = frame_up + NB_BYTE_LINE * NB_LINE;
172     }
173 
174   return;
175 
176 #endif
177 
178 }
179 
180 /*****************************************************************************
181 
182     Function: edit_ram
183 
184     Description: view or edit the RAM
185     Parameters: none
186     Return: nothing
187 
188 *****************************************************************************/
189 void
edit_ram()190 edit_ram ()
191 {
192 
193 #ifdef ALLEGRO
194 
195   BITMAP *bg;
196   unsigned char line, col;
197   char *tmp_buf = (char *) alloca (100);
198   unsigned short dum;
199 
200   bg = create_bitmap (vheight, vwidth);
201   blit (screen, bg, 0, 0, 0, 0, vheight, vwidth);
202 
203   selected_byte = 0;
204   out = 0;
205   frame_up = 0;
206   frame_down = frame_up + NB_LINE * NB_BYTE_LINE;
207 
208   while (!out)
209     {
210       clear (screen);
211       for (line = 0; line < NB_LINE; line++)
212 	{
213 	  sprintf (tmp_buf, "%04X", frame_up + line * NB_BYTE_LINE);
214 	  textoutshadow (screen, font, tmp_buf, blit_x, blit_y + 10 * line,
215 			 -15, 2, 1, 1);
216 	  for (col = 0; col < NB_BYTE_LINE / 2; col++)
217 	    {
218 	      if ((dum = frame_up + line * NB_BYTE_LINE + col) ==
219 		  selected_byte)
220 		rectfill (screen, blit_x + (6 + col * 3) * 8,
221 			  blit_y + 10 * line - 1, blit_x + (8 + col * 3) * 8,
222 			  blit_y + 10 * (line + 1) - 2, -15);
223 	      sprintf (tmp_buf, "%02X", RAM[dum]);
224 	      textoutshadow (screen, font, tmp_buf,
225 			     blit_x + (6 + col * 3) * 8, blit_y + 10 * line,
226 			     -1, 2, 1, 1);
227 	    }
228 	  for (; col < NB_BYTE_LINE; col++)
229 	    {
230 	      if ((dum = frame_up + line * NB_BYTE_LINE + col) ==
231 		  selected_byte)
232 		rectfill (screen, blit_x + (8 + col * 3) * 8,
233 			  blit_y + 10 * line - 1, blit_x + (10 + col * 3) * 8,
234 			  blit_y + 10 * (line + 1) - 2, -15);
235 	      sprintf (tmp_buf, "%02X",
236 		       RAM[frame_up + line * NB_BYTE_LINE + col]);
237 	      textoutshadow (screen, font, tmp_buf,
238 			     blit_x + (8 + col * 3) * 8, blit_y + 10 * line,
239 			     -1, 2, 1, 1);
240 	    }
241 
242 	}
243 
244       ram_key ();
245       vsync ();
246     }
247 
248   blit (bg, screen, 0, 0, 0, 0, vheight, vwidth);
249   destroy_bitmap (bg);
250   return;
251 
252 #endif
253 
254 }
255