summaryrefslogtreecommitdiffstats
path: root/gui/avr8emu
blob: cae4ebc705ee1ed72d07d99b4606e148bfbb1c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
#
#   AVR-8 emulator GUI
#
#   Copyright (C) 2007 Michael Buesch <mb@bu3sch.de>
#
#   This program is free software; you can redistribute it and/or
#   modify it under the terms of the GNU General Public License
#   as published by the Free Software Foundation; either version 2
#   of the License, or (at your option) any later version.
#
#   This program is distributed in the hope that it will be useful,
#   but WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#   GNU General Public License for more details.

AVR8EMU_VERSION = "001"

import tempfile
import sys
import os
import pyavr8emu as emu
from PyQt4.QtCore import *
from PyQt4.QtGui import *


#FIXME
inst = emu.Instance(0)


class EmulatorStatus(QTextBrowser):
	def __init__(self, parent=None):
		QTextBrowser.__init__(self, parent)
		self.setFontFamily("Courier")
		self.setFontPointSize(10)
		self.setLineWrapMode(QTextEdit.NoWrap)

		self.timer = QTimer()
		self.timer.setInterval(300)
		self.timer.setSingleShot(False)
		self.connect(self.timer, SIGNAL("timeout()"), self.pollMsgs)
		self.timer.start()

	def pollMsgs(self):
		while True:
			msg = inst.pollStatusMsg()
			if not msg:
				return
			self.append(msg[:-1])

class CodeEditor(QTextEdit):
	def __init__(self, parent):
		QTextEdit.__init__(self, parent)
		self.setFontFamily("Courier")
		self.setFontPointSize(12)
		self.setLineWrapMode(QTextEdit.NoWrap)

	def statusBar(self):
		return self.parent().statusBar()

	def loadFile(self, filename):
		fd = QFile(filename)
		if not fd.open(QIODevice.ReadOnly):
			self.statusBar().showMessage("Could not open file %s" % filename)
			return
		data = fd.readAll()
		fd.close()
		if not data:
			self.statusBar().showMessage("Could not read file %s" % filename)
			return
		self.setText(str(data))

class CentralWidget(QWidget):
	def __init__(self, mainwnd):
		QWidget.__init__(self, mainwnd)

		self.mainwnd = mainwnd
		l = QVBoxLayout(self)
		sp = QSplitter(self)
		sp.setOrientation(Qt.Vertical)
		self.editor = CodeEditor(self)
		sp.addWidget(self.editor)
		self.emuStatus = EmulatorStatus(self)
		sp.addWidget(self.emuStatus)
		l.addWidget(sp)

	def statusBar(self):
		return self.parent().statusBar()

	def loadFile(self):
		fn = QFileDialog.getOpenFileName(self, "Open assembly file")
		if not fn:
			return
		inst.loadFlash(file(fn).read())
#FIXME		self.editor.loadFile(fn)

	def __assemble(self, assembly):
		#FIXME not portable
		asmfile = tempfile.NamedTemporaryFile()
		asmfile.write(assembly)
		asmfile.flush()
		os.system("avra %s" % asmfile.name)
		hexfile = asmfile.name + ".hex"
		binfile = asmfile.name + ".bin"
		os.system("avr-objcopy -I ihex -O binary %s %s" % (hexfile, binfile))
		fd = file(binfile)
		bindata = fd.read()
		fd.close()
		os.system("rm %s.*" % asmfile.name)
		print "Got %d bytes binary" % len(bindata)
		return bindata

	def reset(self):
		inst.reset()

class StatusBar(QStatusBar):
	def showMessage(self, msg):
		QStatusBar.showMessage(self, msg, 10000)

class MainWindow(QMainWindow):
	def __init__(self, parent=None):
		QMainWindow.__init__(self, parent)
		self.setWindowTitle("Fast! Atmel AVR 8-bit emulator")

		self.setCentralWidget(CentralWidget(self))
		self.setStatusBar(StatusBar(self))

		mb = QMenuBar(self)
		filemen = QMenu("&File", mb)
		filemen.addAction("&Load...", self.centralWidget().loadFile)
		filemen.addAction("&Reset", self.centralWidget().reset)
		filemen.addSeparator()
		filemen.addAction("&Exit", self.close)
		mb.addMenu(filemen)
		mb.addSeparator()
		helpmen = QMenu("&Help", mb)
		helpmen.addAction("&About", self.about)
		mb.addMenu(helpmen)

		self.setMenuBar(mb)

	def about(self):
		QMessageBox.information(self, "About",
					"Fast! Atmel AVR 8-bit emulator\n"
					"Version %s\n"
					"Copyright (c) 2007 Michael Buesch"
					% AVR8EMU_VERSION)

def main():
	app = QApplication(sys.argv)
	mainwnd = MainWindow()
	mainwnd.show()
	sys.exit(app.exec_())

if __name__ == "__main__":
	main()
bues.ch cgit interface