⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 memory.c

📁 GDB Remote Stub Backend for debugging an embedded ARM system via JTAG common hardware debug interfac
💻 C
字号:
/* *  Copyright (C) 2004 Tobias Lorenz * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. *//* memory functions */#include <stdio.h>#include <stdlib.h>#include "debug.h"#include "jtag.h"/* memory cache variables */unsigned long memory_cache_valid = 0;unsigned long memory_cache_address;unsigned long memory_cache_data;void memory_preload(){    debug_exec_begin();    debug_exec(0xe1a00000, 0, DEBUG_SPEED);	/* NOP */    /* Run at system speed */    debug_exec(0xe1a00000, 0, SYSTEM_SPEED);    jtag_instruction(JTAG_RESTART, NULL, 0);    debug_exec_begin();    debug_exec(0xe1a00000, 0, DEBUG_SPEED);	/* NOP */    debug_exec(0xe1a00000, 0, DEBUG_SPEED);	/* NOP */}void memory_address_check(unsigned long address){    if (address % 4 != 0) {	printf("WARNING: Address % 4 != 0\n");	exit(1);    }}void memory_peek(unsigned long address, unsigned long *data){    unsigned long alignment = address % 4;        /* Reading from a not 32-bit aligned address */    if (alignment) {	unsigned long address32 = address & ~(0x03);	unsigned long data0;	unsigned long data4;		memory_peek(address32+0, &data0);	memory_peek(address32+4, &data4);	printf("memory_peek: Alignment %i\n", address%4);	switch (alignment) {	    case 1:	*data = ((data0 & 0x00ffffff) <<  8) | ((data4 & 0xff000000) >> 24); break;	    case 2:	*data = ((data0 & 0x0000ffff) << 16) | ((data4 & 0xffff0000) >> 16); break;	    case 3:	*data = ((data0 & 0x000000ff) << 24) | ((data4 & 0xffffff00) >>  8); break;	    default:	printf("memory_peek: Alignment error\n");	}		return;    }    /* Check cache for valid data */    if (memory_cache_valid && (memory_cache_address == address)) {	printf("*");	*data = memory_cache_data;	return;    }    /* Do the read */    // memory_address_check(address);    debug_exec_begin();    debug_exec(0xe5900000, 0      , DEBUG_SPEED);	/* LDR r0, [r0]		- put address in r0 */    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    debug_exec(0xe1a00000, address, DEBUG_SPEED);	/* NOP 			- scan in address */    debug_exec(0xe4901000, 0      , DEBUG_SPEED);	/* LDR r1, [r0]		- load r1 from location of r0 */    /* Run at system speed */    debug_exec(0xe1a00000, 0      , SYSTEM_SPEED);    jtag_instruction(JTAG_RESTART, NULL, 0);    debug_exec_begin();    debug_exec(0xe4801000, 0      , DEBUG_SPEED);	/* STR r1, [r0]		- output the data value */    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    *data =    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    /* Cache last value */    memory_cache_valid   = 1;    memory_cache_address = address;    memory_cache_data    = *data;}void memory_poke(unsigned long address, unsigned long data){    unsigned long alignment = address % 4;    /* Writing from a not 32-bit aligned address */    if (alignment) {	unsigned long address32 = address & ~(0x03);	unsigned long data0;	unsigned long data4;		memory_peek(address32+0, &data0);	memory_peek(address32+4, &data4);	printf("memory_poke: Alignment %i\n", address%4);	switch (alignment) {	    case 1:	data0 &= 0xff000000;			data4 &= 0x00ffffff;			data0 |= (data & 0xffffff00) >>  8;			data4 |= (data & 0x000000ff) << 24;			break;	    case 2:	data0 &= 0xffff0000;			data4 &= 0x0000ffff;			data0 |= (data & 0xffff0000) >> 16;			data4 |= (data & 0x0000ffff) << 16;			break;	    case 3:	data0 &= 0xffffff00;			data4 &= 0x000000ff;			data0 |= (data & 0xff000000) >> 24;			data4 |= (data & 0x00ffffff) <<  8;			break;	    default:	printf("memory_poke: Alignment error\n");	}	memory_poke(address32+0, data0);	memory_poke(address32+4, data4);		return;    }    memory_address_check(address);    debug_exec_begin();    debug_exec(0xe8900003, 0      , DEBUG_SPEED);	/* LDMIA r0, [r0, r1]	- put address and data */    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    debug_exec(0xe1a00000, 0      , DEBUG_SPEED);	/* NOP */    debug_exec(0xe1a00000, address, DEBUG_SPEED);	/* NOP 			- scan in address */    debug_exec(0xe1a00000, data   , DEBUG_SPEED);	/* NOP 			- scan in data */    debug_exec(0xe4801000, 0, DEBUG_SPEED);		/* STR r1, [r0]		- store r1 in location of r0 */    /* Run at system speed */    debug_exec(0xe1a00000, 0, SYSTEM_SPEED);    jtag_instruction(JTAG_RESTART, NULL, 0);    //do {    //    usleep(100);    //} while (!ice_halted());    /* Cache last value */    memory_cache_valid   = 1;    memory_cache_address = address;    memory_cache_data    = data;}void memory_read(unsigned long address, unsigned long *data, unsigned long length){    unsigned long temp;    memory_cache_valid = 0;    memory_preload();    while (length-- > 0) {	memory_peek(address, data);		/* increase the pointers */	address += 4;	data    ++;    }}void memory_write(unsigned long address, unsigned long *data, unsigned long length){    unsigned long temp;    memory_cache_valid = 0;    memory_preload();    while (length-- > 0) {	memory_poke(address, *data);		/* increase the pointers */	address += 4;	data    ++;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -