summaryrefslogtreecommitdiffstats
path: root/awlsimcli
blob: 9849f9b4e16a2832228115784a6256fca1843eaa (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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# AWL simulator - Commandline interface
# Copyright 2012-2013 Michael Buesch <m@bues.ch>
#
# Licensed under the terms of the GNU General Public License version 2.
#

import getopt

from awlsim import *
from awlsim.cpu import *


opt_onecycle = False
opt_fouraccu = False
opt_quiet = False
opt_extInsns = False
opt_rng = False
opt_mnemonics = S7CPUSpecs.MNEMONICS_AUTO


def usage():
	printInfo("awlsim version %d.%d" % (VERSION_MAJOR, VERSION_MINOR))
	printInfo("")
	printInfo("%s [OPTIONS] AWL-source" % sys.argv[0])
	printInfo("")
	printInfo("Options:")
	printInfo(" -1|--onecycle         Only run one cycle")
	printInfo(" -4|--fouraccu         Enable 4-accu mode")
	printInfo(" -q|--quiet            No status messages")
	printInfo(" -x|--extended-insns   Enable extended instructions")
	printInfo(" -r|--rng 1/0          Enable/disable the RNG (Default: off)")
	printInfo(" -m|--mnemonics auto   Mnemonics type: en, de, auto (default)")

def writeStdout(message):
	if not opt_quiet:
		sys.stdout.write(message)
		sys.stdout.flush()

nextScreenUpdate = 0.0
lastDump = ""

def blockExitCallback(cpu):
	global nextScreenUpdate
	global lastDump

	now = time.time()
	if now < nextScreenUpdate and\
	   not opt_onecycle:
		return
	nextScreenUpdate = now + 0.1
	dump = str(cpu)
	# Pad lines
	dump = [ line + (79 - len(line)) * ' ' + '|'
		 for line in dump.splitlines() ]
	dump = '\n'.join(dump)
	lastDump = dump
	writeStdout("\x1B[H" + dump)

def main():
	global opt_onecycle
	global opt_fouraccu
	global opt_quiet
	global opt_extInsns
	global opt_rng
	global opt_mnemonics

	try:
		(opts, args) = getopt.getopt(sys.argv[1:],
			"h1qxr:m:",
			[ "help", "onecycle", "quiet", "extended-insns",
			  "rng=", "mnemonics=", ])
	except getopt.GetoptError as e:
		printError(str(e))
		usage()
		return 1
	for (o, v) in opts:
		if o in ("-h", "--help"):
			usage()
			return 0
		if o in ("-1", "--onecycle"):
			opt_onecycle = True
		if o in ("-4", "--fouraccu"):
			opt_fouraccu = True
		if o in ("-q", "--quiet"):
			opt_quiet = True
		if o in ("-x", "--extended-insns"):
			opt_extInsns = True
		if o in ("-r", "--rng"):
			opt_rng = str2bool(v)
		if o in ("-m", "--mnemonics"):
			if v.lower() == "en":
				opt_mnemonics = S7CPUSpecs.MNEMONICS_EN
			elif v.lower() == "de":
				opt_mnemonics = S7CPUSpecs.MNEMONICS_DE
			elif v.lower() == "auto":
				opt_mnemonics = S7CPUSpecs.MNEMONICS_AUTO
			else:
				printError("-m|--mnemonics: Invalid mnemonics type")
				sys.exit(1)
	if len(args) != 1:
		usage()
		return 1
	awlSource = args[0]

	try:
		p = AwlParser()
		p.parseFile(awlSource)
		s = AwlSim()
		cpu = s.getCPU()
		specs = cpu.getSpecs()
		specs.setConfiguredMnemonics(opt_mnemonics)
		specs.setNrAccus(4 if opt_fouraccu else 2)
		cpu.enableRNG(opt_rng)
		cpu.enableExtendedInsns(opt_extInsns)
		cpu.setBlockExitCallback(blockExitCallback, cpu)
		s.load(p.getParseTree())
		try:
			writeStdout("\x1B[?25l\x1B[2J")
			while 1:
				s.runCycle()
				if opt_onecycle:
					break
		finally:
			writeStdout("\x1B[?25h\x1B[2J\x1B[H")
			writeStdout(lastDump + '\n')
	except (AwlSimError, AwlParserError) as e:
		printError("-- AWL simulator error --")
		printError(str(e))
		return 1
	except KeyboardInterrupt as e:
		pass
	return 0

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