target.c 109 KB
Newer Older
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
			LOG_ERROR("mem2array: Read @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
			Jim_AppendStrings(interp, Jim_GetResult(interp), "mem2array: cannot read memory", NULL);
			e = JIM_ERR;
			len = 0;
		} else {
			v = 0; /* shut up gcc */
			for (i = 0 ;i < count ;i++, n++) {
				switch (width) {
					case 4:
						v = target_buffer_get_u32(target, &buffer[i*width]);
						break;
					case 2:
						v = target_buffer_get_u16(target, &buffer[i*width]);
						break;
					case 1:
						v = buffer[i] & 0x0ff;
						break;
				}
				new_int_array_element(interp, varname, n, v);
			}
			len -= count;
		}
	}
oharboe's avatar
oharboe committed
3025

3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}

static int get_int_array_element(Jim_Interp * interp, const char *varname, int idx, u32 *val)
{
	char *namebuf;
	Jim_Obj *nameObjPtr, *valObjPtr;
	int result;
	long l;

	namebuf = alloc_printf("%s(%d)", varname, idx);
	if (!namebuf)
		return JIM_ERR;

	nameObjPtr = Jim_NewStringObj(interp, namebuf, -1);
	if (!nameObjPtr)
	{
		free(namebuf);
		return JIM_ERR;
	}

	Jim_IncrRefCount(nameObjPtr);
	valObjPtr = Jim_GetVariable(interp, nameObjPtr, JIM_ERRMSG);
	Jim_DecrRefCount(interp, nameObjPtr);
	free(namebuf);
	if (valObjPtr == NULL)
		return JIM_ERR;

	result = Jim_GetLong(interp, valObjPtr, &l);
	/* printf("%s(%d) => 0%08x\n", varname, idx, val); */
	*val = l;
	return result;
}

static int jim_array2mem(Jim_Interp *interp, int argc, Jim_Obj *const *argv)
{
	command_context_t *context;
3065
	target_t *target;
3066

3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
	context = Jim_GetAssocData(interp, "context");
	if (context == NULL){
		LOG_ERROR("array2mem: no command context");
		return JIM_ERR;
	}
	target = get_current_target(context);
	if (target == NULL){
		LOG_ERROR("array2mem: no current target");
		return JIM_ERR;
	}
3077

3078
	return target_array2mem( interp,target, argc-1, argv+1 );
3079
}
3080

3081
3082
static int target_array2mem(Jim_Interp *interp, target_t *target, int argc, Jim_Obj *const *argv)
{
3083
3084
	long l;
	u32 width;
3085
	int len;
3086
3087
3088
3089
3090
	u32 addr;
	u32 count;
	u32 v;
	const char *varname;
	u8 buffer[4096];
3091
3092
	int  n, e, retval;
	u32 i;
3093
3094
3095

	/* argv[1] = name of array to get the data
	 * argv[2] = desired width
oharboe's avatar
oharboe committed
3096
	 * argv[3] = memory address
3097
3098
	 * argv[4] = count to write
	 */
3099
	if (argc != 4) {
3100
3101
3102
		Jim_WrongNumArgs(interp, 1, argv, "varname width addr nelems");
		return JIM_ERR;
	}
3103
	varname = Jim_GetString(argv[0], &len);
3104
3105
	/* given "foo" get space for worse case "foo(%d)" .. add 20 */

3106
	e = Jim_GetLong(interp, argv[1], &l);
3107
3108
3109
3110
	width = l;
	if (e != JIM_OK) {
		return e;
	}
oharboe's avatar
oharboe committed
3111

3112
	e = Jim_GetLong(interp, argv[2], &l);
3113
3114
3115
3116
	addr = l;
	if (e != JIM_OK) {
		return e;
	}
3117
	e = Jim_GetLong(interp, argv[3], &l);
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
	len = l;
	if (e != JIM_OK) {
		return e;
	}
	switch (width) {
		case 8:
			width = 1;
			break;
		case 16:
			width = 2;
			break;
		case 32:
			width = 4;
			break;
		default:
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
			Jim_AppendStrings( interp, Jim_GetResult(interp), "Invalid width param, must be 8/16/32", NULL );
			return JIM_ERR;
	}
	if (len == 0) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: zero width read?", NULL);
		return JIM_ERR;
	}
	if ((addr + (len * width)) < addr) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: addr + len - wraps to zero?", NULL);
		return JIM_ERR;
	}
	/* absurd transfer size? */
	if (len > 65536) {
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
		Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: absurd > 64K item request", NULL);
		return JIM_ERR;
oharboe's avatar
oharboe committed
3152
3153
	}

3154
3155
3156
3157
3158
3159
3160
	if ((width == 1) ||
		((width == 2) && ((addr & 1) == 0)) ||
		((width == 4) && ((addr & 3) == 0))) {
		/* all is well */
	} else {
		char buf[100];
		Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
oharboe's avatar
oharboe committed
3161
		sprintf(buf, "array2mem address: 0x%08x is not aligned for %d byte reads", addr, width);
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
		Jim_AppendStrings(interp, Jim_GetResult(interp), buf , NULL);
		return JIM_ERR;
	}

	/* Transfer loop */

	/* index counter */
	n = 0;
	/* assume ok */
	e = JIM_OK;
	while (len) {
		/* Slurp... in buffer size chunks */
oharboe's avatar
oharboe committed
3174

3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
		count = len; /* in objects.. */
		if (count > (sizeof(buffer)/width)) {
			count = (sizeof(buffer)/width);
		}

		v = 0; /* shut up gcc */
		for (i = 0 ;i < count ;i++, n++) {
			get_int_array_element(interp, varname, n, &v);
			switch (width) {
			case 4:
				target_buffer_set_u32(target, &buffer[i*width], v);
				break;
			case 2:
				target_buffer_set_u16(target, &buffer[i*width], v);
				break;
			case 1:
				buffer[i] = v & 0x0ff;
				break;
			}
		}
		len -= count;

zwelch's avatar
zwelch committed
3197
		retval = target_write_memory(target, addr, width, count, buffer);
3198
3199
3200
3201
		if (retval != ERROR_OK) {
			/* BOO !*/
			LOG_ERROR("array2mem: Write @ 0x%08x, w=%d, cnt=%d, failed", addr, width, count);
			Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));
3202
			Jim_AppendStrings(interp, Jim_GetResult(interp), "array2mem: cannot read memory", NULL);
3203
3204
3205
3206
			e = JIM_ERR;
			len = 0;
		}
	}
oharboe's avatar
oharboe committed
3207

3208
3209
3210
3211
	Jim_SetResult(interp, Jim_NewEmptyStringObj(interp));

	return JIM_OK;
}
3212

3213
void target_all_handle_event( enum target_event e )
3214
3215
3216
{
	target_t *target;

3217
	LOG_DEBUG( "**all*targets: event: %d, %s",
3218
3219
			e,
			Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
3220
3221
3222
3223
3224
3225
3226
3227

	target = all_targets;
	while (target){
		target_handle_event( target, e );
		target = target->next;
	}
}

3228
void target_handle_event( target_t *target, enum target_event e )
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
{
	target_event_action_t *teap;
	int done;

	teap = target->event_action;

	done = 0;
	while( teap ){
		if( teap->event == e ){
			done = 1;
			LOG_DEBUG( "target: (%d) %s (%s) event: %d (%s) action: %s\n",
					   target->target_number,
					   target->cmd_name,
zwelch's avatar
zwelch committed
3242
					   target_get_name(target),
3243
3244
3245
					   e,
					   Jim_Nvp_value2name_simple( nvp_target_event, e )->name,
					   Jim_GetString( teap->body, NULL ) );
3246
3247
3248
3249
			if (Jim_EvalObj( interp, teap->body )!=JIM_OK)
			{
				Jim_PrintErrorMessage(interp);
			}
3250
3251
3252
3253
		}
		teap = teap->next;
	}
	if( !done ){
3254
		LOG_DEBUG( "event: %d %s - no action",
3255
3256
3257
3258
3259
3260
3261
				   e,
				   Jim_Nvp_value2name_simple( nvp_target_event, e )->name );
	}
}

enum target_cfg_param {
	TCFG_TYPE,
3262
	TCFG_EVENT,
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
	TCFG_WORK_AREA_VIRT,
	TCFG_WORK_AREA_PHYS,
	TCFG_WORK_AREA_SIZE,
	TCFG_WORK_AREA_BACKUP,
	TCFG_ENDIAN,
	TCFG_VARIANT,
	TCFG_CHAIN_POSITION,
};

static Jim_Nvp nvp_config_opts[] = {
	{ .name = "-type",             .value = TCFG_TYPE },
	{ .name = "-event",            .value = TCFG_EVENT },
	{ .name = "-work-area-virt",   .value = TCFG_WORK_AREA_VIRT },
	{ .name = "-work-area-phys",   .value = TCFG_WORK_AREA_PHYS },
	{ .name = "-work-area-size",   .value = TCFG_WORK_AREA_SIZE },
	{ .name = "-work-area-backup", .value = TCFG_WORK_AREA_BACKUP },
	{ .name = "-endian" ,          .value = TCFG_ENDIAN },
	{ .name = "-variant",          .value = TCFG_VARIANT },
	{ .name = "-chain-position",   .value = TCFG_CHAIN_POSITION },
3282

3283
3284
	{ .name = NULL, .value = -1 }
};
3285

3286
static int target_configure( Jim_GetOptInfo *goi, target_t *target )
3287
3288
3289
3290
3291
3292
3293
3294
{
	Jim_Nvp *n;
	Jim_Obj *o;
	jim_wide w;
	char *cp;
	int e;

	/* parse config or cget options ... */
3295
	while( goi->argc > 0 ){
3296
		Jim_SetEmptyResult( goi->interp );
3297
		/* Jim_GetOpt_Debug( goi ); */
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330

		if( target->type->target_jim_configure ){
			/* target defines a configure function */
			/* target gets first dibs on parameters */
			e = (*(target->type->target_jim_configure))( target, goi );
			if( e == JIM_OK ){
				/* more? */
				continue;
			}
			if( e == JIM_ERR ){
				/* An error */
				return e;
			}
			/* otherwise we 'continue' below */
		}
		e = Jim_GetOpt_Nvp( goi, nvp_config_opts, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( goi, nvp_config_opts, 0 );
			return e;
		}
		switch( n->value ){
		case TCFG_TYPE:
			/* not setable */
			if( goi->isconfigure ){
				Jim_SetResult_sprintf( goi->interp, "not setable: %s", n->name );
				return JIM_ERR;
			} else {
			no_params:
				if( goi->argc != 0 ){
					Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "NO PARAMS");
					return JIM_ERR;
				}
			}
zwelch's avatar
zwelch committed
3331
			Jim_SetResultString( goi->interp, target_get_name(target), -1 );
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
			/* loop for more */
			break;
		case TCFG_EVENT:
			if( goi->argc == 0 ){
				Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ...");
				return JIM_ERR;
			}

			e = Jim_GetOpt_Nvp( goi, nvp_target_event, &n );
			if( e != JIM_OK ){
				Jim_GetOpt_NvpUnknown( goi, nvp_target_event, 1 );
				return e;
			}

			if( goi->isconfigure ){
3347
				if( goi->argc != 1 ){
3348
3349
					Jim_WrongNumArgs( goi->interp, goi->argc, goi->argv, "-event ?event-name? ?EVENT-BODY?");
					return JIM_ERR;
3350
				}
3351
3352
3353
3354
3355
3356
3357
			} else {
				if( goi->argc != 0 ){
					Jim_WrongNumArgs(goi->interp, goi->argc, goi->argv, "-event ?event-name?");
					return JIM_ERR;
				}
			}

3358
			{
3359
				target_event_action_t *teap;
3360

3361
3362
3363
				teap = target->event_action;
				/* replace existing? */
				while( teap ){
3364
					if( teap->event == (enum target_event)n->value ){
3365
3366
3367
3368
						break;
					}
					teap = teap->next;
				}
3369

3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
				if( goi->isconfigure ){
					if( teap == NULL ){
						/* create new */
						teap = calloc( 1, sizeof(*teap) );
					}
					teap->event = n->value;
					Jim_GetOpt_Obj( goi, &o );
					if( teap->body ){
						Jim_DecrRefCount( interp, teap->body );
					}
					teap->body  = Jim_DuplicateObj( goi->interp, o );
					/*
3382
					 * FIXME:
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
					 *     Tcl/TK - "tk events" have a nice feature.
					 *     See the "BIND" command.
					 *    We should support that here.
					 *     You can specify %X and %Y in the event code.
					 *     The idea is: %T - target name.
					 *     The idea is: %N - target number
					 *     The idea is: %E - event name.
					 */
					Jim_IncrRefCount( teap->body );

					/* add to head of event list */
					teap->next = target->event_action;
					target->event_action = teap;
					Jim_SetEmptyResult(goi->interp);
				} else {
					/* get */
					if( teap == NULL ){
						Jim_SetEmptyResult( goi->interp );
					} else {
						Jim_SetResult( goi->interp, Jim_DuplicateObj( goi->interp, teap->body ) );
					}
				}
			}
			/* loop for more */
			break;

		case TCFG_WORK_AREA_VIRT:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				target->working_area_virt = w;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3421
			}
3422
3423
3424
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_virt ) );
			/* loop for more */
			break;
3425

3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
		case TCFG_WORK_AREA_PHYS:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				target->working_area_phys = w;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3438
			}
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_phys ) );
			/* loop for more */
			break;

		case TCFG_WORK_AREA_SIZE:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				target->working_area_size = w;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3455
			}
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
			Jim_SetResult( interp, Jim_NewIntObj( goi->interp, target->working_area_size ) );
			/* loop for more */
			break;

		case TCFG_WORK_AREA_BACKUP:
			if( goi->isconfigure ){
				target_free_all_working_areas(target);
				e = Jim_GetOpt_Wide( goi, &w );
				if( e != JIM_OK ){
					return e;
				}
				/* make this exactly 1 or 0 */
				target->backup_working_area = (!!w);
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3473
			}
3474
			Jim_SetResult(interp, Jim_NewIntObj(goi->interp, target->backup_working_area));
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
			/* loop for more e*/
			break;

		case TCFG_ENDIAN:
			if( goi->isconfigure ){
				e = Jim_GetOpt_Nvp( goi, nvp_target_endian, &n );
				if( e != JIM_OK ){
					Jim_GetOpt_NvpUnknown( goi, nvp_target_endian, 1 );
					return e;
				}
				target->endianness = n->value;
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
			}
			n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
			if( n->name == NULL ){
				target->endianness = TARGET_LITTLE_ENDIAN;
				n = Jim_Nvp_value2name_simple( nvp_target_endian, target->endianness );
3495
			}
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
			Jim_SetResultString( goi->interp, n->name, -1 );
			/* loop for more */
			break;

		case TCFG_VARIANT:
			if( goi->isconfigure ){
				if( goi->argc < 1 ){
					Jim_SetResult_sprintf( goi->interp,
										   "%s ?STRING?",
										   n->name );
					return JIM_ERR;
				}
				if( target->variant ){
					free((void *)(target->variant));
				}
				e = Jim_GetOpt_String( goi, &cp, NULL );
				target->variant = strdup(cp);
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3517
			}
3518
3519
3520
3521
3522
			Jim_SetResultString( goi->interp, target->variant,-1 );
			/* loop for more */
			break;
		case TCFG_CHAIN_POSITION:
			if( goi->isconfigure ){
3523
3524
				Jim_Obj *o;
				jtag_tap_t *tap;
3525
				target_free_all_working_areas(target);
3526
				e = Jim_GetOpt_Obj( goi, &o );
3527
3528
3529
				if( e != JIM_OK ){
					return e;
				}
3530
				tap = jtag_tap_by_jim_obj( goi->interp, o );
3531
				if( tap == NULL ){
3532
					return JIM_ERR;
3533
				}
3534
				/* make this exactly 1 or 0 */
3535
				target->tap = tap;
3536
3537
3538
3539
			} else {
				if( goi->argc != 0 ){
					goto no_params;
				}
3540
			}
3541
			Jim_SetResultString( interp, target->tap->dotted_name, -1 );
3542
3543
3544
			/* loop for more e*/
			break;
		}
3545
	} /* while( goi->argc ) */
3546
3547


3548
		/* done - we return */
3549
3550
3551
3552
	return JIM_OK;
}

/** this is the 'tcl' handler for the target specific command */
3553
static int tcl_target_func( Jim_Interp *interp, int argc, Jim_Obj *const *argv )
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
{
	Jim_GetOptInfo goi;
	jim_wide a,b,c;
	int x,y,z;
	u8  target_buf[32];
	Jim_Nvp *n;
	target_t *target;
	struct command_context_s *cmd_ctx;
	int e;

	enum {
		TS_CMD_CONFIGURE,
		TS_CMD_CGET,
3567

3568
3569
3570
3571
		TS_CMD_MWW, TS_CMD_MWH, TS_CMD_MWB,
		TS_CMD_MDW, TS_CMD_MDH, TS_CMD_MDB,
		TS_CMD_MRW, TS_CMD_MRH, TS_CMD_MRB,
		TS_CMD_MEM2ARRAY, TS_CMD_ARRAY2MEM,
3572
		TS_CMD_EXAMINE,
3573
3574
3575
3576
3577
3578
		TS_CMD_POLL,
		TS_CMD_RESET,
		TS_CMD_HALT,
		TS_CMD_WAITSTATE,
		TS_CMD_EVENTLIST,
		TS_CMD_CURSTATE,
3579
		TS_CMD_INVOKE_EVENT,
3580
	};
3581

3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
	static const Jim_Nvp target_options[] = {
		{ .name = "configure", .value = TS_CMD_CONFIGURE },
		{ .name = "cget", .value = TS_CMD_CGET },
		{ .name = "mww", .value = TS_CMD_MWW },
		{ .name = "mwh", .value = TS_CMD_MWH },
		{ .name = "mwb", .value = TS_CMD_MWB },
		{ .name = "mdw", .value = TS_CMD_MDW },
		{ .name = "mdh", .value = TS_CMD_MDH },
		{ .name = "mdb", .value = TS_CMD_MDB },
		{ .name = "mem2array", .value = TS_CMD_MEM2ARRAY },
		{ .name = "array2mem", .value = TS_CMD_ARRAY2MEM },
		{ .name = "eventlist", .value = TS_CMD_EVENTLIST },
		{ .name = "curstate",  .value = TS_CMD_CURSTATE },

		{ .name = "arp_examine", .value = TS_CMD_EXAMINE },
		{ .name = "arp_poll", .value = TS_CMD_POLL },
		{ .name = "arp_reset", .value = TS_CMD_RESET },
		{ .name = "arp_halt", .value = TS_CMD_HALT },
		{ .name = "arp_waitstate", .value = TS_CMD_WAITSTATE },
3601
		{ .name = "invoke-event", .value = TS_CMD_INVOKE_EVENT },
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617

		{ .name = NULL, .value = -1 },
	};

	/* go past the "command" */
	Jim_GetOpt_Setup( &goi, interp, argc-1, argv+1 );

	target = Jim_CmdPrivData( goi.interp );
	cmd_ctx = Jim_GetAssocData(goi.interp, "context");

	/* commands here are in an NVP table */
	e = Jim_GetOpt_Nvp( &goi, target_options, &n );
	if( e != JIM_OK ){
		Jim_GetOpt_NvpUnknown( &goi, target_options, 0 );
		return e;
	}
3618
	/* Assume blank result */
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
	Jim_SetEmptyResult( goi.interp );

	switch( n->value ){
	case TS_CMD_CONFIGURE:
		if( goi.argc < 2 ){
			Jim_WrongNumArgs( goi.interp, goi.argc, goi.argv, "missing: -option VALUE ...");
			return JIM_ERR;
		}
		goi.isconfigure = 1;
		return target_configure( &goi, target );
	case TS_CMD_CGET:
		// some things take params
		if( goi.argc < 1 ){
			Jim_WrongNumArgs( goi.interp, 0, goi.argv, "missing: ?-option?");
			return JIM_ERR;
		}
		goi.isconfigure = 0;
		return target_configure( &goi, target );
		break;
	case TS_CMD_MWW:
	case TS_CMD_MWH:
	case TS_CMD_MWB:
		/* argv[0] = cmd
		 * argv[1] = address
		 * argv[2] = data
		 * argv[3] = optional count.
		 */
3646

3647
3648
3649
3650
3651
3652
3653
		if( (goi.argc == 3) || (goi.argc == 4) ){
			/* all is well */
		} else {
		mwx_error:
			Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR DATA [COUNT]", n->name );
			return JIM_ERR;
		}
3654

3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
		e = Jim_GetOpt_Wide( &goi, &a );
		if( e != JIM_OK ){
			goto mwx_error;
		}

		e = Jim_GetOpt_Wide( &goi, &b );
		if( e != JIM_OK ){
			goto mwx_error;
		}
		if( goi.argc ){
			e = Jim_GetOpt_Wide( &goi, &c );
			if( e != JIM_OK ){
				goto mwx_error;
			}
		} else {
			c = 1;
		}
3672

3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
		switch( n->value ){
		case TS_CMD_MWW:
			target_buffer_set_u32( target, target_buf, b );
			b = 4;
			break;
		case TS_CMD_MWH:
			target_buffer_set_u16( target, target_buf, b );
			b = 2;
			break;
		case TS_CMD_MWB:
			target_buffer_set_u8( target, target_buf, b );
			b = 1;
			break;
		}
		for( x = 0 ; x < c ; x++ ){
zwelch's avatar
zwelch committed
3688
			e = target_write_memory( target, a, b, 1, target_buf );
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
			if( e != ERROR_OK ){
				Jim_SetResult_sprintf( interp, "Error writing @ 0x%08x: %d\n", (int)(a), e );
				return JIM_ERR;
			}
			/* b = width */
			a = a + b;
		}
		return JIM_OK;
		break;

		/* display */
	case TS_CMD_MDW:
	case TS_CMD_MDH:
	case TS_CMD_MDB:
		/* argv[0] = command
		 * argv[1] = address
		 * argv[2] = optional count
		 */
		if( (goi.argc == 2) || (goi.argc == 3) ){
			Jim_SetResult_sprintf( goi.interp, "expected: %s ADDR [COUNT]", n->name );
			return JIM_ERR;
		}
		e = Jim_GetOpt_Wide( &goi, &a );
		if( e != JIM_OK ){
			return JIM_ERR;
		}
		if( goi.argc ){
			e = Jim_GetOpt_Wide( &goi, &c );
			if( e != JIM_OK ){
				return JIM_ERR;
			}
		} else {
			c = 1;
		}
		b = 1; /* shut up gcc */
		switch( n->value ){
		case TS_CMD_MDW:
			b =  4;
			break;
		case TS_CMD_MDH:
			b = 2;
			break;
		case TS_CMD_MDB:
			b = 1;
			break;
		}

		/* convert to "bytes" */
		c = c * b;
		/* count is now in 'BYTES' */
		while( c > 0 ){
			y = c;
			if( y > 16 ){
				y = 16;
			}
zwelch's avatar
zwelch committed
3744
			e = target_read_memory( target, a, b, y / b, target_buf );
3745
3746
3747
3748
			if( e != ERROR_OK ){
				Jim_SetResult_sprintf( interp, "error reading target @ 0x%08lx", (int)(a) );
				return JIM_ERR;
			}
3749

3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
			Jim_fprintf( interp, interp->cookie_stdout, "0x%08x ", (int)(a) );
			switch( b ){
			case 4:
				for( x = 0 ; (x < 16) && (x < y) ; x += 4 ){
					z = target_buffer_get_u32( target, &(target_buf[ x * 4 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%08x ", (int)(z) );
				}
				for( ; (x < 16) ; x += 4 ){
					Jim_fprintf( interp, interp->cookie_stdout, "         " );
				}
				break;
			case 2:
				for( x = 0 ; (x < 16) && (x < y) ; x += 2 ){
					z = target_buffer_get_u16( target, &(target_buf[ x * 2 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%04x ", (int)(z) );
				}
				for( ; (x < 16) ; x += 2 ){
					Jim_fprintf( interp, interp->cookie_stdout, "     " );
				}
				break;
			case 1:
			default:
				for( x = 0 ; (x < 16) && (x < y) ; x += 1 ){
					z = target_buffer_get_u8( target, &(target_buf[ x * 4 ]) );
					Jim_fprintf( interp, interp->cookie_stdout, "%02x ", (int)(z) );
				}
				for( ; (x < 16) ; x += 1 ){
					Jim_fprintf( interp, interp->cookie_stdout, "   " );
				}
				break;
			}
			/* ascii-ify the bytes */
			for( x = 0 ; x < y ; x++ ){
3783
				if( (target_buf[x] >= 0x20) &&
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
					(target_buf[x] <= 0x7e) ){
					/* good */
				} else {
					/* smack it */
					target_buf[x] = '.';
				}
			}
			/* space pad  */
			while( x < 16 ){
				target_buf[x] = ' ';
				x++;
			}
			/* terminate */
			target_buf[16] = 0;
			/* print - with a newline */
			Jim_fprintf( interp, interp->cookie_stdout, "%s\n", target_buf );
			/* NEXT... */
			c -= 16;
			a += 16;
		}
		return JIM_OK;
	case TS_CMD_MEM2ARRAY:
		return target_mem2array( goi.interp, target, goi.argc, goi.argv );
		break;
	case TS_CMD_ARRAY2MEM:
		return target_array2mem( goi.interp, target, goi.argc, goi.argv );
		break;
	case TS_CMD_EXAMINE:
		if( goi.argc ){
3813
			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3814
3815
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3816
3817
		if (!target->tap->enabled)
			goto err_tap_disabled;
3818
3819
3820
3821
3822
3823
3824
3825
		e = target->type->examine( target );
		if( e != ERROR_OK ){
			Jim_SetResult_sprintf( interp, "examine-fails: %d", e );
			return JIM_ERR;
		}
		return JIM_OK;
	case TS_CMD_POLL:
		if( goi.argc ){
3826
			Jim_WrongNumArgs( goi.interp, 2, argv, "[no parameters]");
3827
3828
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3829
3830
		if (!target->tap->enabled)
			goto err_tap_disabled;
3831
		if( !(target_was_examined(target)) ){
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
			e = ERROR_TARGET_NOT_EXAMINED;
		} else {
			e = target->type->poll( target );
		}
		if( e != ERROR_OK ){
			Jim_SetResult_sprintf( interp, "poll-fails: %d", e );
			return JIM_ERR;
		} else {
			return JIM_OK;
		}
		break;
	case TS_CMD_RESET:
3844
3845
		if( goi.argc != 2 ){
			Jim_WrongNumArgs( interp, 2, argv, "t|f|assert|deassert BOOL");
3846
3847
3848
3849
3850
3851
3852
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_assert, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( &goi, nvp_assert, 1 );
			return e;
		}
3853
		/* the halt or not param */
3854
3855
3856
3857
		e = Jim_GetOpt_Wide( &goi, &a);
		if( e != JIM_OK ){
			return e;
		}
zwelch's avatar
zwelch committed
3858
3859
		if (!target->tap->enabled)
			goto err_tap_disabled;
3860
		/* determine if we should halt or not. */
3861
		target->reset_halt = !!a;
3862
		/* When this happens - all workareas are invalid. */
3863
3864
		target_free_all_working_areas_restore(target, 0);

3865
		/* do the assert */
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
		if( n->value == NVP_ASSERT ){
			target->type->assert_reset( target );
		} else {
			target->type->deassert_reset( target );
		}
		return JIM_OK;
	case TS_CMD_HALT:
		if( goi.argc ){
			Jim_WrongNumArgs( goi.interp, 0, argv, "halt [no parameters]");
			return JIM_ERR;
		}
zwelch's avatar
zwelch committed
3877
3878
		if (!target->tap->enabled)
			goto err_tap_disabled;
3879
3880
3881
		target->type->halt( target );
		return JIM_OK;
	case TS_CMD_WAITSTATE:
3882
		/* params:  <name>  statename timeoutmsecs */
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
		if( goi.argc != 2 ){
			Jim_SetResult_sprintf( goi.interp, "%s STATENAME TIMEOUTMSECS", n->name );
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_target_state, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( &goi, nvp_target_state,1 );
			return e;
		}
		e = Jim_GetOpt_Wide( &goi, &a );
		if( e != JIM_OK ){
			return e;
		}
zwelch's avatar
zwelch committed
3896
3897
		if (!target->tap->enabled)
			goto err_tap_disabled;
3898
		e = target_wait_state( target, n->value, a );
3899
		if( e != ERROR_OK ){
3900
			Jim_SetResult_sprintf( goi.interp,
3901
								   "target: %s wait %s fails (%d) %s",
3902
3903
								   target->cmd_name,
								   n->name,
3904
								   e, target_strerror_safe(e) );
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
			return JIM_ERR;
		} else {
			return JIM_OK;
		}
	case TS_CMD_EVENTLIST:
		/* List for human, Events defined for this target.
		 * scripts/programs should use 'name cget -event NAME'
		 */
		{
			target_event_action_t *teap;
			teap = target->event_action;
			command_print( cmd_ctx, "Event actions for target (%d) %s\n",
3917
						   target->target_number,
3918
3919
3920
3921
						   target->cmd_name );
			command_print( cmd_ctx, "%-25s | Body", "Event");
			command_print( cmd_ctx, "------------------------- | ----------------------------------------");
			while( teap ){
3922
				command_print( cmd_ctx,
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
							   "%-25s | %s",
							   Jim_Nvp_value2name_simple( nvp_target_event, teap->event )->name,
							   Jim_GetString( teap->body, NULL ) );
				teap = teap->next;
			}
			command_print( cmd_ctx, "***END***");
			return JIM_OK;
		}
	case TS_CMD_CURSTATE:
		if( goi.argc != 0 ){
			Jim_WrongNumArgs( goi.interp, 0, argv, "[no parameters]");
			return JIM_ERR;
		}
3936
		Jim_SetResultString( goi.interp,
3937
3938
							 Jim_Nvp_value2name_simple(nvp_target_state,target->state)->name,-1);
		return JIM_OK;
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
	case TS_CMD_INVOKE_EVENT:
		if( goi.argc != 1 ){
			Jim_SetResult_sprintf( goi.interp, "%s ?EVENTNAME?",n->name);
			return JIM_ERR;
		}
		e = Jim_GetOpt_Nvp( &goi, nvp_target_event, &n );
		if( e != JIM_OK ){
			Jim_GetOpt_NvpUnknown( &goi, nvp_target_event, 1 );
			return e;
		}
		target_handle_event( target, n->value );
		return JIM_OK;
3951
3952
	}
	return JIM_ERR;
zwelch's avatar
zwelch committed
3953
3954
3955
3956

err_tap_disabled:
	Jim_SetResult_sprintf(interp, "[TAP is disabled]");
	return JIM_ERR;
3957
3958
}

3959
static int target_create( Jim_GetOptInfo *goi )
3960
{
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
	Jim_Obj *new_cmd;
	Jim_Cmd *cmd;
	const char *cp;
	char *cp2;
	int e;
	int x;
	target_t *target;
	struct command_context_s *cmd_ctx;

	cmd_ctx = Jim_GetAssocData(goi->interp, "context");
	if( goi->argc < 3 ){
		Jim_WrongNumArgs( goi->interp, 1, goi->argv, "?name? ?type? ..options...");
		return JIM_ERR;
	}

	/* COMMAND */
	Jim_GetOpt_Obj( goi, &new_cmd );
	/* does this command exist? */
	cmd = Jim_GetCommand( goi->interp, new_cmd, JIM_ERRMSG );
	if( cmd ){
		cp = Jim_GetString( new_cmd, NULL );
		Jim_SetResult_sprintf(goi->interp, "Command/target: %s Exists", cp);
		return JIM_ERR;
	}
3985

3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
	/* TYPE */
	e = Jim_GetOpt_String( goi, &cp2, NULL );
	cp = cp2;
	/* now does target type exist */
	for( x = 0 ; target_types[x] ; x++ ){
		if( 0 == strcmp( cp, target_types[x]->name ) ){
			/* found */
			break;
		}
	}
	if( target_types[x] == NULL ){
		Jim_SetResult_sprintf( goi->interp, "Unknown target type %s, try one of ", cp );
		for( x = 0 ; target_types[x] ; x++ ){
			if( target_types[x+1] ){
4000
				Jim_AppendStrings( goi->interp,
For faster browsing, not all history is shown. View entire blame