comparison debug.c @ 2394:340299a76db7

Add debugger load command
author Michael Pavone <pavone@retrodev.com>
date Wed, 06 Dec 2023 22:25:49 -0800
parents 5f4917b9ecfa
children ebca8ab02701
comparison
equal deleted inserted replaced
2393:5f4917b9ecfa 2394:340299a76db7
148 array->get = user_array_get; 148 array->get = user_array_get;
149 array->set = user_array_set; 149 array->set = user_array_set;
150 array->append = user_array_append; 150 array->append = user_array_append;
151 array->size = size; 151 array->size = size;
152 array->storage = size ? size : 4; 152 array->storage = size ? size : 4;
153 array->base = calloc(size, sizeof(debug_val)); 153 array->base = calloc(array->storage, sizeof(debug_val));
154 debug_val ret; 154 debug_val ret;
155 ret.type = DBG_VAL_ARRAY; 155 ret.type = DBG_VAL_ARRAY;
156 ret.v.u32 = array - arrays; 156 ret.v.u32 = array - arrays;
157 return ret; 157 return ret;
158 } 158 }
541 } 541 }
542 free(e->right); 542 free(e->right);
543 } else { 543 } else {
544 free_expr(e->right); 544 free_expr(e->right);
545 } 545 }
546 if (e->op.type == TOKEN_NAME) { 546 if (e->op.type == TOKEN_NAME || e->op.type == TOKEN_ARRAY) {
547 free(e->op.v.str); 547 free(e->op.v.str);
548 } 548 }
549 } 549 }
550 550
551 static void free_expr(expr *e) 551 static void free_expr(expr *e)
3154 cleanup: 3154 cleanup:
3155 fclose(f); 3155 fclose(f);
3156 return 1; 3156 return 1;
3157 } 3157 }
3158 3158
3159 static uint8_t cmd_load(debug_root *root, parsed_command *cmd)
3160 {
3161 char size = cmd->format ? cmd->format[0] : 'b';
3162 if (size != 'b' && size != 'w' && size != 'l') {
3163 fprintf(stderr, "Invalid size %s\n", cmd->format);
3164 return 1;
3165 }
3166 FILE *f = fopen(cmd->args[0].raw, "rb");
3167 if (!f) {
3168 fprintf(stderr, "Failed to open %s for reading\n", cmd->args[0].raw);
3169 return 1;
3170 }
3171 uint32_t start = 0;
3172 debug_val val;
3173 debug_array * arr = NULL;
3174 if (cmd->args[1].parsed->type == EXPR_MEM) {
3175
3176 if (!eval_expr(root, cmd->args[1].parsed->left, &val)) {
3177 fprintf(stderr, "Failed to eval start index\n");
3178 goto cleanup;
3179 }
3180 if (!debug_cast_int(val, &start)) {
3181 fprintf(stderr, "Start index must evaluate to integer\n");
3182 goto cleanup;
3183 }
3184 if (cmd->args[1].parsed->right) {
3185 if (!eval_expr(root, cmd->args[1].parsed->right, &val)) {
3186 fprintf(stderr, "Failed to eval array name in argument %s\n", cmd->args[1].raw);
3187 goto cleanup;
3188 }
3189 arr = get_array(val);
3190 if (!arr) {
3191 fprintf(stderr, "Name in argument %s did not evaluate to an array\n", cmd->args[1].raw);
3192 goto cleanup;
3193 }
3194 }
3195 } else {
3196 if (!eval_expr(root, cmd->args[1].parsed, &val)) {
3197 fprintf(stderr, "Failed to eval %s\n", cmd->args[1].raw);
3198 goto cleanup;
3199 }
3200 arr = get_array(val);
3201 if (!arr) {
3202 fprintf(stderr, "Argument %s did not evaluate to an array\n", cmd->args[1].raw);
3203 goto cleanup;
3204 }
3205 }
3206 uint32_t count = 0;
3207 uint8_t has_count = 0;
3208 if (cmd->num_args > 2) {
3209 if (!eval_expr(root, cmd->args[2].parsed, &val)) {
3210 fprintf(stderr, "Failed to eval %s\n", cmd->args[2].raw);
3211 goto cleanup;
3212 }
3213 if (!debug_cast_int(val, &count)) {
3214 fprintf(stderr, "Count must evaluate to integer\n");
3215 goto cleanup;
3216 }
3217 has_count = 1;
3218 }
3219 union {
3220 uint8_t b[1024];
3221 uint16_t w[512];
3222 uint32_t l[256];
3223 } buffer;
3224 uint32_t cur = start;
3225 if (size == 'l') {
3226 while (count || !has_count)
3227 {
3228 uint32_t n = (has_count && count < 256) ? count : 256;
3229 count -= n;
3230 n = fread(buffer.l, sizeof(uint32_t), n, f);
3231 if (!n) {
3232 break;
3233 }
3234 for (uint32_t i = 0; i < n ; i++)
3235 {
3236 if (arr) {
3237 val = debug_int(buffer.l[i]);
3238 if (cur >= arr->size) {
3239 if (arr->append) {
3240 arr->append(arr, val);
3241 } else {
3242 goto cleanup;
3243 }
3244 } else {
3245 arr->set(arr, cur, val);
3246 }
3247 cur++;
3248 } else {
3249 if (!root->write_mem(root, cur, buffer.l[i], 'l')) {
3250 goto cleanup;
3251 }
3252 cur += 4;
3253 }
3254 }
3255 }
3256 } else if (size == 'w') {
3257 while (count || !has_count)
3258 {
3259 uint32_t n = (has_count && count < 512) ? count : 512;
3260 count -= n;
3261 n = fread(buffer.w, sizeof(uint16_t), n, f);
3262 if (!n) {
3263 break;
3264 }
3265 for (uint32_t i = 0; i < n ; i++)
3266 {
3267 if (arr) {
3268 val = debug_int(buffer.w[i]);
3269 if (cur >= arr->size) {
3270 if (arr->append) {
3271 arr->append(arr, val);
3272 } else {
3273 goto cleanup;
3274 }
3275 } else {
3276 arr->set(arr, cur, val);
3277 }
3278 cur++;
3279 } else {
3280 if (!root->write_mem(root, cur, buffer.w[i], 'w')) {
3281 goto cleanup;
3282 }
3283 cur += 2;
3284 }
3285 }
3286 }
3287 } else {
3288 while (count || !has_count)
3289 {
3290 uint32_t n = (has_count && count < 1024) ? count : 1024;
3291 count -= n;
3292 n = fread(buffer.b, sizeof(uint8_t), n, f);
3293 if (!n) {
3294 break;
3295 }
3296 for (uint32_t i = 0; i < n ; i++)
3297 {
3298 if (arr) {
3299 val = debug_int(buffer.b[i]);
3300 if (cur >= arr->size) {
3301 if (arr->append) {
3302 arr->append(arr, val);
3303 } else {
3304 goto cleanup;
3305 }
3306 } else {
3307 arr->set(arr, cur, val);
3308 }
3309 } else {
3310 if (!root->write_mem(root, cur, buffer.b[i], 'b')) {
3311 goto cleanup;
3312 }
3313 }
3314 cur++;
3315 }
3316 }
3317 }
3318 cleanup:
3319 fclose(f);
3320 return 1;
3321 }
3322
3159 static uint8_t cmd_delete_m68k(debug_root *root, parsed_command *cmd) 3323 static uint8_t cmd_delete_m68k(debug_root *root, parsed_command *cmd)
3160 { 3324 {
3161 uint32_t index; 3325 uint32_t index;
3162 if (!debug_cast_int(cmd->args[0].value, &index)) { 3326 if (!debug_cast_int(cmd->args[0].value, &index)) {
3163 fprintf(stderr, "Argument to delete must be an integer\n"); 3327 fprintf(stderr, "Argument to delete must be an integer\n");
3804 "save", NULL 3968 "save", NULL
3805 }, 3969 },
3806 .usage = "save[/SIZE] FILENAME ARRAY [COUNT]", 3970 .usage = "save[/SIZE] FILENAME ARRAY [COUNT]",
3807 .desc = "Saves COUNT elements of size SIZE from the array or memory region specified by ARRAY to a file", 3971 .desc = "Saves COUNT elements of size SIZE from the array or memory region specified by ARRAY to a file",
3808 .impl = cmd_save, 3972 .impl = cmd_save,
3973 .min_args = 2,
3974 .max_args = 3,
3975 .skip_eval = 1
3976 },
3977 {
3978 .names = (const char *[]){
3979 "load", NULL
3980 },
3981 .usage = "load[/SIZE] FILENAME ARRAY [COUNT]",
3982 .desc = "Loads COUNT elements of size SIZE from a file to the array or memory region specified by ARRAY",
3983 .impl = cmd_load,
3809 .min_args = 2, 3984 .min_args = 2,
3810 .max_args = 3, 3985 .max_args = 3,
3811 .skip_eval = 1 3986 .skip_eval = 1
3812 } 3987 }
3813 }; 3988 };