freemyipod r558 - Code Review

Jump to: navigation, search
Repository:freemyipod
Revision:r557‎ | r558 | r559 >
Date:14:21, 7 February 2011
Author:theseven
Status:new
Tags:
Comment:
New app: Clock gate analysis tool
Modified paths:
  • /apps/clockgatehunter (added) (history)
  • /apps/clockgatehunter/Makefile (added) (history)
  • /apps/clockgatehunter/SOURCES (added) (history)
  • /apps/clockgatehunter/ls.x (added) (history)
  • /apps/clockgatehunter/main.c (added) (history)
  • /apps/clockgatehunter/version.h (added) (history)

Diff [purge]

Index: apps/clockgatehunter/SOURCES
@@ -0,0 +1 @@
 2+main.c
Index: apps/clockgatehunter/ls.x
@@ -0,0 +1,42 @@
 2+ENTRY(__emcore_entrypoint)
 3+OUTPUT_FORMAT(elf32-littlearm)
 4+OUTPUT_ARCH(arm)
 5+
 6+MEMORY
 7+{
 8+ VIRTUAL : ORIGIN = 0x00000000, LENGTH = 0x10000000
 9+}
 10+
 11+SECTIONS
 12+{
 13+ .text :
 14+ {
 15+ __emcore_app_base = .;
 16+ KEEP(.emcoreentrypoint*)
 17+ *(.emcoreentrypoint*)
 18+ *(.text*)
 19+ *(.glue_7)
 20+ *(.glue_7t)
 21+ . = ALIGN(0x10);
 22+ } > VIRTUAL
 23+
 24+ .data :
 25+ {
 26+ *(.rodata*)
 27+ . = ALIGN(0x4);
 28+ *(.data*)
 29+ . = ALIGN(0x4);
 30+ } > VIRTUAL
 31+
 32+ .bss (NOLOAD) :
 33+ {
 34+ *(.bss*)
 35+ *(COMMON)
 36+ } > VIRTUAL
 37+
 38+ /DISCARD/ :
 39+ {
 40+ *(.eh_frame)
 41+ }
 42+
 43+}
Index: apps/clockgatehunter/main.c
@@ -0,0 +1,122 @@
 2+#include "emcoreapp.h"
 3+
 4+
 5+struct wakeup eventwakeup;
 6+int pos = 64;
 7+int state[65];
 8+
 9+void handler(void* user, enum button_event eventtype, int which, int value)
 10+{
 11+ bool action = false;
 12+ switch (eventtype)
 13+ {
 14+ case BUTTON_PRESS:
 15+ switch (which)
 16+ {
 17+ case 0:
 18+ state[pos] = !state[pos];
 19+ action = true;
 20+ break;
 21+ case 1:
 22+ pos++;
 23+ action = true;
 24+ break;
 25+ case 2:
 26+ pos--;
 27+ action = true;
 28+ break;
 29+ }
 30+ break;
 31+ case WHEEL_FORWARD:
 32+ pos++;
 33+ action = true;
 34+ break;
 35+ case WHEEL_BACKWARD:
 36+ pos--;
 37+ action = true;
 38+ break;
 39+ }
 40+ while (pos < 0) pos += 65;
 41+ while (pos > 65) pos -= 65;
 42+ if (action) wakeup_signal(&eventwakeup);
 43+}
 44+
 45+static void renderline(void* framebuf, int width, int fontwidth, int fontheight, int line)
 46+{
 47+ int i;
 48+ for (i = 0; i < 16; i++)
 49+ renderchar(framebuf, 2 + fontwidth * (i + i / 4), 2 + fontheight * (2 + line), width,
 50+ pos == i + line * 16 ? 0xffffffff : 0xff000000,
 51+ pos == i + line * 16 ? 0xff000000 : 0xffffffff,
 52+ *("01X" + state[i + line * 16]));
 53+}
 54+
 55+static void main()
 56+{
 57+ int i, j;
 58+ char buf[9];
 59+ for (i = 0; i < 64; i++) state[i] = clockgate_get_state(i);
 60+ state[i] = false;
 61+ uint32_t orig[2] = {0xffffffff, 0xffffffff};
 62+ uint32_t now[2];
 63+ for (i = 0; i < 2; i++)
 64+ for (j = 0; j < 32; j++)
 65+ if (state[i * 32 + j])
 66+ orig[i] &= ~(1 << j);
 67+ cprintf(3, "Initial state: %08X %08X\n", orig[0], orig[1]);
 68+ int fontwidth = get_font_width();
 69+ int fontheight = get_font_height();
 70+ int width = 19 * fontwidth + 4;
 71+ int height = 6 * fontheight + 4;
 72+ int xoffs = (lcd_get_width() - width) / 2;
 73+ int yoffs = (lcd_get_height() - height) / 2;
 74+ int framebufsize = width * height * 3;
 75+ void* framebuf = malloc(framebufsize);
 76+ if (!framebuf) panicf(PANIC_KILLTHREAD, "Could not allocate framebuffer!");
 77+ memset(framebuf, 0xff, framebufsize);
 78+ memset(framebuf, 0, width * 3);
 79+ memset(framebuf + (height - 1) * width * 3, 0, width * 3);
 80+ for (i = 0; i < height; i++)
 81+ {
 82+ char* ptr = (char*)framebuf + (i * width - 1) * 3;
 83+ for (j = 0; j < 6; j++) *ptr++ = 0;
 84+ }
 85+ wakeup_init(&eventwakeup);
 86+ wakeup_signal(&eventwakeup);
 87+ struct button_hook_entry* hook = button_register_handler(handler, NULL);
 88+ if (!hook) panicf(PANIC_KILLTHREAD, "Could not register button hook!");
 89+ while (true)
 90+ {
 91+ wakeup_wait(&eventwakeup, TIMEOUT_BLOCK);
 92+ now[0] = 0xffffffff;
 93+ now[1] = 0xffffffff;
 94+ for (i = 0; i < 2; i++)
 95+ for (j = 0; j < 32; j++)
 96+ {
 97+ bool oldstate = state[i * 32 + j];
 98+ clockgate_enable(i * 32 + j, oldstate);
 99+ state[i * 32 + j] = clockgate_get_state(i * 32 + j);
 100+ if (state[i * 32 + j] != oldstate) state[i * 32 + j] = 2;
 101+ if (state[i * 32 + j]) now[i] &= ~(1 << j);
 102+ }
 103+ for (i = 0; i < 4; i++) renderline(framebuf, width, fontwidth, fontheight, i);
 104+ renderchar(framebuf, 2 + fontwidth * 18, 2, width, pos == 64 ? 0xffffffff : 0xff000000,
 105+ pos == 64 ? 0xff000000 : 0xffffffff, 'X');
 106+ snprintf(buf, sizeof(buf), "%08X", orig[0]);
 107+ rendertext(framebuf, 2, 2, width, 0xff000000, 0xffffffff, buf);
 108+ snprintf(buf, sizeof(buf), "%08X", orig[1]);
 109+ rendertext(framebuf, 2 + 9 * fontwidth, 2, width, 0xff000000, 0xffffffff, buf);
 110+ snprintf(buf, sizeof(buf), "%08X", now[0]);
 111+ rendertext(framebuf, 2, 2 + fontheight, width, 0xff000000, 0xffffffff, buf);
 112+ snprintf(buf, sizeof(buf), "%08X", now[1]);
 113+ rendertext(framebuf, 2 + 9 * fontwidth, 2 + fontheight, width,
 114+ 0xff000000, 0xffffffff, buf);
 115+ displaylcd(xoffs, yoffs, width, height, framebuf, 0, 0, width);
 116+ if (state[64]) break;
 117+ }
 118+ button_unregister_handler(hook);
 119+ cprintf(3, "Final state: %08X %08X\n", now[0], now[1]);
 120+}
 121+
 122+
 123+EMCORE_APP_HEADER("Clock gate hunter", main, 127)
Index: apps/clockgatehunter/version.h
@@ -0,0 +1,36 @@
 2+//
 3+//
 4+// Copyright 2010 TheSeven
 5+//
 6+//
 7+// This file is part of emBIOS.
 8+//
 9+// emBIOS is free software: you can redistribute it and/or
 10+// modify it under the terms of the GNU General Public License as
 11+// published by the Free Software Foundation, either version 2 of the
 12+// License, or (at your option) any later version.
 13+//
 14+// emBIOS is distributed in the hope that it will be useful,
 15+// but WITHOUT ANY WARRANTY; without even the implied warranty of
 16+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 17+// See the GNU General Public License for more details.
 18+//
 19+// You should have received a copy of the GNU General Public License along
 20+// with emBIOS. If not, see <http://www.gnu.org/licenses/>.
 21+//
 22+//
 23+
 24+
 25+#ifndef __VERSION_H__
 26+#define __VERSION_H__
 27+
 28+
 29+#define VERSION "0.0.1pre"
 30+#define VERSION_MAJOR 0
 31+#define VERSION_MINOR 0
 32+#define VERSION_PATCH 1
 33+#define VERSION_SVN "$REVISION$"
 34+#define VERSION_SVN_INT $REVISIONINT$
 35+
 36+
 37+#endif
\ No newline at end of file
Index: apps/clockgatehunter/Makefile
@@ -0,0 +1,120 @@
 2+NAME := clockgatehunter
 3+STACKSIZE := 4096
 4+COMPRESS := true
 5+
 6+EMCOREDIR ?= ../../emcore/trunk/
 7+
 8+ifeq ($(shell uname),WindowsNT)
 9+CCACHE :=
 10+else
 11+CCACHE := $(shell which ccache)
 12+endif
 13+
 14+CROSS ?= arm-elf-eabi-
 15+CC := $(CCACHE) $(CROSS)gcc
 16+AS := $(CROSS)as
 17+LD := $(CROSS)ld
 18+OBJCOPY := $(CROSS)objcopy
 19+ELF2ECA := $(CROSS)elf2emcoreapp
 20+
 21+LIBINCLUDES :=
 22+
 23+CFLAGS += -Os -fno-pie -fno-stack-protector -fomit-frame-pointer -I. -I$(EMCOREDIR)/export $(LIBINCLUDES) -ffunction-sections -fdata-sections -mcpu=arm940t -DARM_ARCH=4
 24+LDFLAGS += "$(shell $(CC) -print-libgcc-file-name)" --emit-relocs --gc-sections
 25+
 26+preprocess = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#")
 27+preprocesspaths = $(shell $(CC) $(PPCFLAGS) $(2) -E -P -x c $(1) | grep -v "^\#" | sed -e "s:^..*:$(dir $(1))&:")
 28+
 29+REVISION := $(shell svnversion .)
 30+REVISIONINT := $(shell echo $(REVISION) | sed -e "s/[^0-9].*$$//")
 31+
 32+HELPERS := build/__emcore_armhelpers.o
 33+
 34+SRC := $(call preprocesspaths,SOURCES,-I. -I..)
 35+OBJ := $(SRC:%.c=build/%.o)
 36+OBJ := $(OBJ:%.S=build/%.o) $(HELPERS)
 37+
 38+all: $(NAME)
 39+
 40+-include $(OBJ:%=%.dep)
 41+
 42+$(NAME): build/$(NAME).emcoreapp
 43+
 44+build/$(NAME).emcoreapp: build/$(NAME).elf
 45+ @echo [EMCAPP] $<
 46+ifeq ($(COMPRESS),true)
 47+ @$(ELF2ECA) -z -s $(STACKSIZE) -o $@ $^
 48+else
 49+ @$(ELF2ECA) -s $(STACKSIZE) -o $@ $^
 50+endif
 51+
 52+build/$(NAME).elf: ls.x $(OBJ)
 53+ @echo [LD] $@
 54+ @$(LD) $(LDFLAGS) -o $@ -T ls.x $(OBJ)
 55+
 56+build/%.o: %.c build/version.h
 57+ @echo [CC] $<
 58+ifeq ($(shell uname),WindowsNT)
 59+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 60+else
 61+ @-mkdir -p $(dir $@)
 62+endif
 63+ @$(CC) -c $(CFLAGS) -o $@ $<
 64+ @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
 65+ @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
 66+ifeq ($(shell uname),WindowsNT)
 67+ @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
 68+else
 69+ @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
 70+endif
 71+ @rm -f $@.dep.tmp
 72+
 73+build/%.o: %.S build/version.h
 74+ @echo [CC] $<
 75+ifeq ($(shell uname),WindowsNT)
 76+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 77+else
 78+ @-mkdir -p $(dir $@)
 79+endif
 80+ @$(CC) -c $(CFLAGS) -o $@ $<
 81+ @$(CC) -MM $(CFLAGS) $< > $@.dep.tmp
 82+ @sed -e "s|.*:|$@:|" < $@.dep.tmp > $@.dep
 83+ifeq ($(shell uname),WindowsNT)
 84+ @sed -e "s/.*://" -e "s/\\$$//" < $@.dep.tmp | fmt -1 | sed -e "s/^ *//" -e "s/$$/:/" >> $@.dep
 85+else
 86+ @sed -e 's/.*://' -e 's/\\$$//' < $@.dep.tmp | fmt -1 | sed -e 's/^ *//' -e 's/$$/:/' >> $@.dep
 87+endif
 88+ @rm -f $@.dep.tmp
 89+
 90+build/__emcore_%.o: $(EMCOREDIR)/export/%.c
 91+ @echo [CC] $<
 92+ifeq ($(shell uname),WindowsNT)
 93+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 94+else
 95+ @-mkdir -p $(dir $@)
 96+endif
 97+ @$(CC) -c $(CFLAGS) -o $@ $<
 98+
 99+build/__emcore_%.o: $(EMCOREDIR)/export/%.S
 100+ @echo [CC] $<
 101+ifeq ($(shell uname),WindowsNT)
 102+ @-if not exist $(subst /,\,$(dir $@)) md $(subst /,\,$(dir $@))
 103+else
 104+ @-mkdir -p $(dir $@)
 105+endif
 106+ @$(CC) -c $(CFLAGS) -o $@ $<
 107+
 108+build/version.h: version.h .svn/entries
 109+ @echo [PP] $<
 110+ifeq ($(shell uname),WindowsNT)
 111+ @-if not exist build md build
 112+ @sed -e "s/\$$REVISION\$$/$(REVISION)/" -e "s/\$$REVISIONINT\$$/$(REVISIONINT)/" < $< > $@
 113+else
 114+ @-mkdir -p build
 115+ @sed -e 's/\$$REVISION\$$/$(REVISION)/' -e 's/\$$REVISIONINT\$$/$(REVISIONINT)/' < $< > $@
 116+endif
 117+
 118+clean:
 119+ @rm -rf build
 120+
 121+.PHONY: all clean $(NAME)
Index: apps/clockgatehunter
Property changes on: apps/clockgatehunter
___________________________________________________________________
Added: svn:ignore
## -0,0 +1 ##
 122+build