def easy1(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( """%s .org $C000 .define PLAYER_X=$200 .define PLAYER_Y=$201 %s forever: jmp forever .org $fffa .dw $C000 .dw $C000 .dw $C000 """%(memmap, code) ) if errors: return None counts = [] for test in [[0],[1],[2],[3],[4],[5],[6],[7],[4,5],[4,6],[4,7],[5,7],[4,5,6,7]]: e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) x = 74 y = 32 e.write(x,0x200) e.write(y,0x201) for t in test: e.controller(1, t) if 4 in test: y-=1 if 5 in test: y+=1 if 6 in test: x-=1 if 7 in test: x+=1 counts.append( run( e, 0x100 ) ) if e.read(0x200) != x or e.read(0x201) != y: return None return rom_size(rom), sum(counts)/len(counts)
def easy2(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( """%s .org $C000 .define TEXT=$E000 %s forever: jmp forever .org $fffa .dw $C000 .dw $C000 .dw $C000 """%(memmap, code) ) if errors: return None rom = rom[:0x2010]+("\xff"*0x500)+rom[0x2510:] counts = [] for test in ["Hello world","Potato skillets"]: e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) for i,c in enumerate(test): e.rom[0x6000+i] = ord(c) counts.append( run( e, 0x10000 ) ) for i,c in enumerate(test): print e.vram[0x2020+i] for i,c in enumerate(test): if e.vram[0x2020+i] != ord(c): return None return rom_size(rom), sum(counts)/len(counts)
def medium1(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( """%s .org $C000 %s forever: jmp forever .org $fffa .dw $C000 .dw $C000 .dw $C000 """%(memmap, code) ) if errors: return None # Testing this one is easy. We simply create lists and then see if the # student's sort matches Python's. We will randomly create 5 lists to # average out "best cases". Granted, this means that it's possible for # a student to beat the "record" by just running the code multiple times, # but more efficient sorts should always win out. counts = [] for test in range(5): e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) l = [] for i in range(64): l.append(random.randint(0,255)) for i in range(64): e.write( l[i], 0x200+i ) counts.append( run( e, 0x10000 ) ) l.sort() for i in range(64): if e.read( 0x200+i) != l[i]: return None return rom_size(rom), sum(counts)/len(counts)
def barcamp1(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( ".org $C000\n"+code + "\nforever:\n jmp forever\n.org $fffa\n.dw $C000\n.dw $C000\n.dw $C000" ) if errors: return None e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) for i in range(0x100): e.step() if e.last_op == 0x60: break if e.X == 160 and e.Y == 100: return rom_size(rom), 100 else: None
def barcamp1(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( ".org $C000\n" + code + "\nforever:\n jmp forever\n.org $fffa\n.dw $C000\n.dw $C000\n.dw $C000" ) if errors: return None e = Emulator(rom[0x10:0x4010], rom[0x4010:]) for i in range(0x100): e.step() if e.last_op == 0x60: break if e.X == 160 and e.Y == 100: return rom_size(rom), 100 else: None
def barcamp3(challenge, student, code, completed): a = Assembler() preamble = ".org $C000\n.define PADDLE_1Y=$204\n" postamble = "\nforever:\n jmp forever\n.org $fffa\n.dw $C000\n.dw $C000\n.dw $C000" rom, errors = a.assemble( preamble+code + postamble ) if errors: None for test in [[0],[4],[5],[4,3]]: e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) for t in test: e.controller(1,t) for i in range(0x100): e.step() if e.last_op == 0x60: break if 4 in test and e.read(0x204) != 1: return None if 5 in test and e.read(0x204) != 0xff: return None return rom_size(rom), 100
def barcamp3(challenge, student, code, completed): a = Assembler() preamble = ".org $C000\n.define PADDLE_1Y=$204\n" postamble = "\nforever:\n jmp forever\n.org $fffa\n.dw $C000\n.dw $C000\n.dw $C000" rom, errors = a.assemble(preamble + code + postamble) if errors: None for test in [[0], [4], [5], [4, 3]]: e = Emulator(rom[0x10:0x4010], rom[0x4010:]) for t in test: e.controller(1, t) for i in range(0x100): e.step() if e.last_op == 0x60: break if 4 in test and e.read(0x204) != 1: return None if 5 in test and e.read(0x204) != 0xff: return None return rom_size(rom), 100
def medium2(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( """%s .org $C000 %s forever: jmp forever .org $fffa .dw $C000 .dw $C000 .dw $C000 """%(memmap, code) ) if errors: return None # In this case, we simply provide a number of initial configurations and # see if the student performs them correctly. counts = [] return rom_size(rom), sum(counts)/len(counts)
def barcamp2(challenge, student, code, completed): a = Assembler() preamble = ".org $C000\n.define BALL_X=$200\n.define BALL_Y=$201\n.define BALL_DX=$202\n.define BALL_DY=$203\n" postamble = "\nforever:\n jmp forever\n.org $fffa\n.dw $C000\n.dw $C000\n.dw $C000" rom, errors = a.assemble( preamble+code + postamble ) if errors: return None for test in [[54,32,1,1],[0,99,0xff,0xff],[55,0xff,0xff,1]]: e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) e.write( test[0], 0x200 ) e.write( test[1], 0x201 ) e.X = test[2] e.Y = test[3] for i in range(0x100): e.step() if e.last_op == 0x60: break if e.read(0x200) != (test[0]+test[2])&0xff or e.read(0x201) != (test[1]+test[3])&0xff: return None return rom_size(rom), 100
def barcamp2(challenge, student, code, completed): a = Assembler() preamble = ".org $C000\n.define BALL_X=$200\n.define BALL_Y=$201\n.define BALL_DX=$202\n.define BALL_DY=$203\n" postamble = "\nforever:\n jmp forever\n.org $fffa\n.dw $C000\n.dw $C000\n.dw $C000" rom, errors = a.assemble(preamble + code + postamble) if errors: return None for test in [[54, 32, 1, 1], [0, 99, 0xff, 0xff], [55, 0xff, 0xff, 1]]: e = Emulator(rom[0x10:0x4010], rom[0x4010:]) e.write(test[0], 0x200) e.write(test[1], 0x201) e.X = test[2] e.Y = test[3] for i in range(0x100): e.step() if e.last_op == 0x60: break if e.read(0x200) != (test[0] + test[2]) & 0xff or e.read( 0x201) != (test[1] + test[3]) & 0xff: return None return rom_size(rom), 100
def easy3(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( """%s .org $C000 .define PLAYER1X=$200 .define PLAYER1Y=$201 .define PLAYER2X=$202 .define PLAYER2Y=$203 .define BALLX=$204 .define BALLY=$205 %s forever: jmp forever .org $fffa .dw $C000 .dw $C000 .dw $C000 """%(memmap, code) ) if errors: return None counts = [] for test in [[41,61,11,13,9,14],[61,11,13,9,14,99]]: p1x,p1y,p2x,p2y,bx,by = test e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) i = 0 while i < 6: e.write( test[i], 0x200+i ) i += 1 counts.append( run( e, 0x200 ) ) if e.oam[0:4] != [0,0x20,0,0]: return None if e.oam[4:8] != [p1y,0x20,1,p1x]: return None if e.oam[8:12] != [p1y+8,0x20,1,p1x]: return None if e.oam[12:16] != [p1y+16,0x20,1,p1x]: return None if e.oam[16:20] != [by,0x20,0,bx]: return None if e.oam[20:24] != [p2y,0x20,2,p2x]: return None if e.oam[24:28] != [p2y+8,0x20,2,p2x]: return None if e.oam[28:32] != [p2y+16,0x20,2,p2x]: return None for a in e.oam[32:]: if a != 0: return None return rom_size(rom), sum(counts)/len(counts)
def hard1(challenge, student, code, completed): a = Assembler() rom, errors = a.assemble( """%s .org $C000 %s forever: jmp forever .org $fffa .dw $C000 .dw $C000 .dw $C000 """%(memmap, code) ) print errors if errors: return None # We do two basic tests. In the first, we simply start with a completely # filled grid. All cells should die. counts = [] e = Emulator( rom[0x10:0x4010], rom[0x4010:] ) for i in range(0x100): e.write( 1, 0x200+i ) counts.append( run( e, 0x100000 ) ) for i in range(0x100): if e.read( 0x200+i ) != 0: return None # This is the real test. Note that we will actually run the program two times # to ensure that there are no side effects. start = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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0x200+i ) counts.append( run( e, 0x100000 ) ) for i in range(0x100): if e.read( 0x200+i ) != stage1[i]: return None e.PC = 0xC000 counts.append( run( e, 0x100000 ) ) for i in range(0x100): if e.read( 0x200+i ) != stage2[i]: return None print "OK" return rom_size(rom), sum(counts)/len(counts)
def setUp(self): self.A = Assembler() self.E = Emulator()