comparison cpu_dsl.py @ 2578:9b01541cbd60

Fix rol and ror in new CPU core
author Michael Pavone <pavone@retrodev.com>
date Fri, 07 Feb 2025 19:58:20 -0800
parents 5f725429d08f
children 939b818df589
comparison
equal deleted inserted replaced
2577:5f725429d08f 2578:9b01541cbd60
544 output.append('\n\t} else {') 544 output.append('\n\t} else {')
545 after = '\n\t}' 545 after = '\n\t}'
546 resultBit = f'({prog.getLastSize()} - {prog.lastB})' 546 resultBit = f'({prog.getLastSize()} - {prog.lastB})'
547 myRes = prog.lastA 547 myRes = prog.lastA
548 elif prog.lastOp.op == 'rol': 548 elif prog.lastOp.op == 'rol':
549 if type(prog.lastB) is int: 549 if type(prog.lastBUnmasked) is int:
550 if prog.lastB == 0: 550 if prog.lastBUnmasked == 0:
551 explicit[flag] = 0 551 explicit[flag] = 0
552 continue 552 continue
553 else: 553 else:
554 output.append(f'\n\tif (!{prog.lastB}) {{') 554 output.append(f'\n\tif (!{prog.lastBUnmasked}) {{')
555 _addExplicitFlagSet(prog, output, flag, 0) 555 _addExplicitFlagSet(prog, output, flag, 0)
556 output.append('\n\t} else {') 556 output.append('\n\t} else {')
557 after = '\n\t}' 557 after = '\n\t}'
558 resultBit = 0 558 resultBit = 0
559 elif prog.lastOp.op == 'ror': 559 elif prog.lastOp.op == 'ror':
560 if type(prog.lastB) is int: 560 if type(prog.lastBUnmasked) is int:
561 if prog.lastB == 0: 561 if prog.lastBUnmasked == 0:
562 explicit[flag] = 0 562 explicit[flag] = 0
563 continue 563 continue
564 else: 564 else:
565 output.append(f'\n\tif (!{prog.lastB}) {{') 565 output.append(f'\n\tif (!{prog.lastBUnmasked}) {{')
566 _addExplicitFlagSet(prog, output, flag, 0) 566 _addExplicitFlagSet(prog, output, flag, 0)
567 output.append('\n\t} else {') 567 output.append('\n\t} else {')
568 after = '\n\t}' 568 after = '\n\t}'
569 resultBit = prog.getLastSize() - 1 569 resultBit = prog.getLastSize() - 1
570 elif prog.lastOp.op == 'neg': 570 elif prog.lastOp.op == 'neg':
974 if flagUpdates: 974 if flagUpdates:
975 for flag in flagUpdates: 975 for flag in flagUpdates:
976 calc = prog.flags.flagCalc[flag] 976 calc = prog.flags.flagCalc[flag]
977 if calc == 'carry': 977 if calc == 'carry':
978 needsCarry = True 978 needsCarry = True
979 decl = ''
980 destSize = prog.paramSize(rawParams[2]) 979 destSize = prog.paramSize(rawParams[2])
981 needsSizeAdjust = False 980 needsSizeAdjust = False
982 if len(params) > 3: 981 if len(params) > 3:
983 size = params[3] 982 size = params[3]
984 if size == 0: 983 if size == 0:
992 needsSizeAdjust = True 991 needsSizeAdjust = True
993 if needsCarry: 992 if needsCarry:
994 prog.sizeAdjust = size 993 prog.sizeAdjust = size
995 else: 994 else:
996 size = destSize 995 size = destSize
997 996 rotMask = size - 1
998 prog.lastB = params[1] 997 if type(params[1]) is int:
998 b = params[1] & rotMask
999 mdecl = ''
1000 ret = ''
1001 else:
1002 mdecl,b = prog.getTemp(prog.paramSize(rawParams[1]))
1003 ret = f'\n\t{b} = {params[1]} & {rotMask};'
1004 prog.lastB = b
1005 prog.lastBUnmasked = params[1]
999 if needsSizeAdjust: 1006 if needsSizeAdjust:
1000 decl,name = prog.getTemp(size) 1007 decl,name = prog.getTemp(size)
1008 mdecl += decl
1001 dst = prog.carryFlowDst = name 1009 dst = prog.carryFlowDst = name
1002 else: 1010 else:
1003 dst = params[2] 1011 dst = params[2]
1004 ret = decl + '\n\t{dst} = {a} << {b} | {a} >> ({size} - {b});'.format(dst = dst, 1012 ret += '\n\t{dst} = {a} << {b} | {a} >> ({size} - {b});'.format(dst = dst,
1005 a = params[0], b = params[1], size=size 1013 a = params[0], b = b, size=size
1006 ) 1014 )
1007 if needsSizeAdjust and not needsCarry: 1015 if needsSizeAdjust and not needsCarry:
1008 mask = (1 << size) - 1 1016 mask = (1 << size) - 1
1009 ret += f'\n\t{params[2]} = ({params[2]} & ~{mask}) | ({dst} & {mask});' 1017 ret += f'\n\t{params[2]} = ({params[2]} & ~{mask}) | ({dst} & {mask});'
1010 return ret 1018 return mdecl + ret
1011 1019
1012 def _rlcCImpl(prog, params, rawParams, flagUpdates): 1020 def _rlcCImpl(prog, params, rawParams, flagUpdates):
1013 needsCarry = False 1021 needsCarry = False
1014 if flagUpdates: 1022 if flagUpdates:
1015 for flag in flagUpdates: 1023 for flag in flagUpdates:
1061 if flagUpdates: 1069 if flagUpdates:
1062 for flag in flagUpdates: 1070 for flag in flagUpdates:
1063 calc = prog.flags.flagCalc[flag] 1071 calc = prog.flags.flagCalc[flag]
1064 if calc == 'carry': 1072 if calc == 'carry':
1065 needsCarry = True 1073 needsCarry = True
1066 decl = ''
1067 destSize = prog.paramSize(rawParams[2]) 1074 destSize = prog.paramSize(rawParams[2])
1068 needsSizeAdjust = False 1075 needsSizeAdjust = False
1069 if len(params) > 3: 1076 if len(params) > 3:
1070 size = params[3] 1077 size = params[3]
1071 if size == 0: 1078 if size == 0:
1079 needsSizeAdjust = True 1086 needsSizeAdjust = True
1080 if needsCarry: 1087 if needsCarry:
1081 prog.sizeAdjust = size 1088 prog.sizeAdjust = size
1082 else: 1089 else:
1083 size = destSize 1090 size = destSize
1084 prog.lastB = params[1] 1091 rotMask = size - 1
1092 if type(params[1]) is int:
1093 b = params[1] & rotMask
1094 mdecl = ''
1095 ret = ''
1096 else:
1097 mdecl,b = prog.getTemp(prog.paramSize(rawParams[1]))
1098 ret = f'\n\t{b} = {params[1]} & {rotMask};'
1099 prog.lastB = b
1100 prog.lastBUnmasked = params[1]
1085 if needsSizeAdjust: 1101 if needsSizeAdjust:
1086 decl,name = prog.getTemp(size) 1102 decl,name = prog.getTemp(size)
1087 dst = prog.carryFlowDst = name 1103 dst = prog.carryFlowDst = name
1104 mdecl += decl
1088 else: 1105 else:
1089 dst = params[2] 1106 dst = params[2]
1090 ret = decl + '\n\t{dst} = {a} >> {b} | {a} << ({size} - {b});'.format(dst = dst, 1107 ret += '\n\t{dst} = {a} >> {b} | {a} << ({size} - {b});'.format(dst = dst,
1091 a = params[0], b = params[1], size=size 1108 a = params[0], b = b, size=size
1092 ) 1109 )
1093 if needsSizeAdjust and not needsCarry: 1110 if needsSizeAdjust and not needsCarry:
1094 mask = (1 << size) - 1 1111 mask = (1 << size) - 1
1095 ret += f'\n\t{params[2]} = ({params[2]} & ~{mask}) | ({dst} & {mask});' 1112 ret += f'\n\t{params[2]} = ({params[2]} & ~{mask}) | ({dst} & {mask});'
1096 return ret 1113 return mdecl + ret
1097 1114
1098 def _rrcCImpl(prog, params, rawParams, flagUpdates): 1115 def _rrcCImpl(prog, params, rawParams, flagUpdates):
1099 needsCarry = False 1116 needsCarry = False
1100 if flagUpdates: 1117 if flagUpdates:
1101 for flag in flagUpdates: 1118 for flag in flagUpdates: